简体   繁体   English

使用matlab,如何找到十进制数字的最后两位?

[英]Using matlab,how to find the last two digits of a decimal number?

How can one find the last two digits of a decimal number using MATLAB ? 如何使用MATLAB查找十进制数的最后两位?

Example: 例:

59  for 1.23000659
35  for 56368.35
12  for 548695412

There will always be issues when you have a decimal number with many integer digits and fractional digits. 如果您的十进制数字包含很多整数和小数位,总会出现问题。 In this case, the number of integer and decimal digits decide if we are correct or not in estimating the last two digits. 在这种情况下,整数和十进制数字的数量决定了我们在估计最后两位数字时是否正确。 Let's take at the code and the comments thereafter. 接下来,让我们看一下代码和注释。

Code

%// num is the input decimal number

t1 = num2str(num,'%1.15e') %// Convert number to exponential notation
t1 = t1(1:strfind(t1,'e')-1)
lastind = find(t1-'0',1,'last')
out = str2num(t1(lastind-1:lastind)) %// desired output

Results and Conclusions 结果与结论

  1. For num = 1.23000659, it prints the output as 59, which is correct thanks to the fact that the number of integer and decimal digits don't add upto more than 16. 对于num = 1.23000659,它将输出显示为59,这是正确的,这是由于整数和十进制数字的总和不超过16。

  2. For num = 56368.35, we get output as 35, which is correct again and the reason is the same as before. 对于num = 56368.35,我们得到的输出为35,这又是正确的,原因与以前相同。

  3. For num = 548695412, we are getting the correct output of 12 because of the same good reason. 对于num = 548695412,由于相同的合理原因,我们得到了正确的输出12。

  4. For an out of the question sample of num = 2736232.3927327329236576 (deliberately chosen a number with many integer and fractional digits), the code run gives output as 33 which is wrong and the reason could be inferred from the fact that integer and decimal digits add upto a much bigger number than the code could handle. 对于num = 2736232.3927327329236576(故意选择一个具有许多整数和小数位的数字)的问题样本,代码运行给出的输出为33,这是错误的,并且可以从整数和十进制数字加起来等于比代码可以处理的数字大得多。

One can look into MATLAB command vpa for getting more precision, if extreme cases like the 4th one are to dealt with. 如果要处理像第4种这样的极端情况,可以研究MATLAB命令vpa以获得更高的精度。

Convert to string and then extract the last two characters: 转换为字符串,然后提取最后两个字符:

x = 1.23;           % x = 1.23
s = num2str(x);     % s = "1.23"
t = s(end-1:end);   % t = "23"
u = str2num(t);     % u = 23

Note: depending on your specific needs you might want to supply a precision or formatSpec to num2str . 注意:根据您的特定需求,您可能需要为formatSpec提供precisionformatSpec

The other two answers are nice and straight forward, but here you have a mathematical way of doing it ;) 另外两个答案很好,很简单,但是在这里,您有一种数学方法;)

Assuming a as your number, 假设a作为你的电话号码,

ashift=0.01*a; %shift the last two digits

afloor=floor(ashift); %crop the last two digits

LastDecimals=a-100*afloor; %substract the cropped number form the original, only the last two digits left.

Of course if you have non-natural numbers, you can figure those out too with the same "floor and subtract technique as above. 当然,如果您有非自然数,则也可以使用与上述相同的“底数和减法”来解决这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM