MATLAB: How to store only 3 digits after the decimal point

precisionroundround function

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 :
x=2.123456789123456789
y=round(x,3),
so I got:
y=2.123000000000000000
But I want it to be (stored) as:
y=2.123
Is what i'm doing write ?
Thank you so much.

Best Answer

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.