Count number of digits after decimal point?

79 views (last 30 days)
Hi,
I want to calculate the number of digits after the decimal point with the trailing zeros. I have tried a lot of methods but no luck.
For example: If I input '0.2500', the output should be '4', means the trailing zeros should be counted as well.

Accepted Answer

John D'Errico
John D'Errico on 14 Mar 2022
MATLAB does not distinguish between numbers with trailing zeros after the decimal point. That is, 0.25 == 0.250 == 0.250000000. They are all identically the same numbers. (I won't even get into the question about if 0.3 is really exactly the fraction 3/10, as many people expect. It is not.)
However, if your number is in character representation, as you have written it, then counting the number of digits after the decimal point is easy. Just locate where the decimal point lies. Then locate the last digit in the number. Assuming the string in question is only one number, and there are no trailing spaces, then this will work:
N = '0.25000';
numel(N) - find(N == '.')
ans = 5
You would need to worry about cases with trailing white space, then worry about cases where there was no decimal point, like the number '25', which has zero digits to the right of the decimal point. The trick there, is if a number has no decimal point, then implicitly, there is a decimal point at the very end. So if there is no decimal point found, then add a decimal point as a new last character to the number.
  3 Comments
John D'Errico
John D'Errico on 14 Mar 2022
I said this in my answer, and as Steve said. Once you create a NUMBER, then the number of trailing zeros is lost.
0.1 == 0.1000000000
ans = logical
1
The two numbers are now identical. It is now impossible to distinguish how many trailing zeros were in there.
And that is why I said you need to keep the numbers in purely character form if you really need to count the number of digits after a decimal point. Even at that, be careful, since counting the number of digits after the decimal point for a number like 0.7 is subtly difficult.
x = 0.7
x = 0.7000
sprintf('%0.55f',x)
ans = '0.6999999999999999555910790149937383830547332763671875000'
So as you see, the number actually stored in a double precision number is not exactly 0.7, but 0.699999999...
And that begs the question, of exaclt how many trailing digits there are in any such number. For example, how many trailing digits are there in this number:
format long g
y = 1/7
y =
0.142857142857143

Sign in to comment.

More Answers (1)

Jan
Jan on 14 Mar 2022
If your input is '0.2500', it is not a number, but a char vector. Then considering the trailing zeros is possible. But if you are talking about numbers, you have to consider the general limitations mentioned by John.
Remember, that numbers are stored in binary format and only displayed in decimal format in the command window. All internal computations are performed in the IEEEE-754 format. Most decimal values do not have an exact binary representation and the same is true the other way around. This causes effects like:
0.1 + 0.2 - 0.3 == 0 % FALSE!
If the output is displayed with 0 decimal digits, 0.0000 is shown, but the real answer differs from 0. How many digits 0.549999999999999 has is a question of taste anbd you cannot get a unique answer.
So please clarify, if you are talking about numbers like 0.2500 or char vectors like '0.2500'. For the latter:
s = '0.2500';
k = find(s ~= '0', 1, 'last')
  2 Comments
Jan
Jan on 14 Mar 2022
Again: For Matlab and other numerical software, there need not be a difference between 5.2000 and 5.1999999999999999 . If you just crop the trailing digits or decide for a certain number of digits for rounding up or down, the result will differ.
You are asking for a more or less arbitrary result.
Think twice about what this means:
0.1 + 0.2 - 0.3 == 0 % FALSE!
You do need another approach. If the number of trailing zeros matter, working with doubles cannot solve the problem. You do have to work with char vectors to represent the decimal digits. Maybe a UINT8 vector for storing the digits is working also.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by