在整数小数重presentation分离数字 [英] Separating digits in decimal representation of integer
问题描述
我想利用用户输入的输入作为一个整数,例如(45697)
,和前两位别的东西存放在数组,向量,或如(4 5 6 9 7)
,这样我就可以再使用一些函数调用来检查的前两个值(4 5)
并对其进行计算。
I want to take user input entered as an integer, for example (45697)
, and store the first two digits in an array, vector, or something else such as ( 4 5 6 9 7 )
, so that I could then use some function call to check for the first two values (4 5)
and perform calculations on them.
问题:我不知道如何存储追回这些前两个值
The Problem: I don't know how to store the recover those first two values.
有一个简单的函数调用?或者,我必须首先存储输入任何数组,然后提取前两个值,如果是这样,怎么样?
Is there a simple function call? Or would I have to store the input first as any array and then extract the first two values, and if so, how?
推荐答案
您可以轻松地使用转换到/从字符串做到这一点:
You can do it easily using conversions to/from strings:
>> x = 45697; % or whatever positive value
>> str = num2str(x);
>> y = [str2num(str(1)) str2num(str(2))]
y =
4 5
这假定号 X
为正(如果它是消极的第一个字符不会是一个数字)。这似乎是的情况下,由于根据你的意见将其重新presents的电阻
This assumes that the number x
is positive (if it were negative the first character would not be a digit). That seems to be the case, since according to your comments it represents an electrical resistance.
这篇关于在整数小数重presentation分离数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!