How to store only 3 digits after the decimal point?

816 views (last 30 days)
I'm working on R2016a and using round function to get red of the extra digits that I don't need on right side of the decimal point, so I did the following :
so I got:
But I want it to be (stored) as:
Is what i'm doing write ?
Thank you so much.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Sep 2017
Edited: Walter Roberson on 21 Sep 2017
MATLAB uses IEEE 754 Binary Double Precision to represent floating point numbers. All floating point scheme that use binary mantissas cannot exactly represent 1/10, just like finite decimal representation schemes cannot exactly represent 1/3 or 1/7 .
IEEE 754 also defined a Decimal Double Precision representation scheme, which can represent 2.123 exactly. However, computing those values in software is much slower. The only systems I know of that implement IEEE 754 Decimal Double Precision in hardware are the IBM z90 series.
If you need a certain specific number of decimal places to be stored, then use rationals with a power-of-10 denominator.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 21 Sep 2017
Edited: James Tursa on 21 Sep 2017
This is just a display difference. The numbers are the same. E.g.,
>> format long
>> x = 2.123456789123456789
x =
>> y = round(x,3)
y =
>> z = 2.123
z =
>> y == z
ans =
>> format short g
>> y
y =
>> z
z =
If you want to display the number to three decimal places, e.g.,
>> fprintf(' %.3f\n',y)
>> fprintf(' %.3f\n',z)
But keep in mind that 2.123 cannot be represented exactly in IEEE double precision format. The nearest IEEE double precision number to 2.123, converted to an exact decimal representation, is
>> num2strexact(2.123)
ans =

Community Treasure Hunt

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

Start Hunting!

Translated by