reduce precision of a number
579 views (last 30 days)
Show older comments
I am having a problem trying to reduce precision output of a number
for example
x = 1.123456 I want x = 1.123 (only 3 values after a decimal and not a string)
I use x = round(x*1000)/1000 and get x = 1.1230 (I dont want the end zero)
I use x = sprintf('%.3f',x) and get a string '1.123' (i dont want a string)
so i use x = str2num(sprintf('%.3f',x)) and get x = 1.1230 (again with the zero)
please help me remove a zero at the end. I am not concerned with precision.
5 Comments
Walter Roberson
on 24 Feb 2024
Moved: Dyuman Joshi
on 25 Feb 2024
The options are
round(X)
or
round(X, N)
or
round(X*10^N)/10^N
or
vpa(X, N) %requires symbolic toolbox
For positive N, the output of round() will not generally happen to be an exact multiple of a power of 2, so the output() of round() will not generally happen to be exactly representable in binary. (It will happen sometimes -- for example round(0.25319,2) will happen to be exactly 0.25 which is exactly representable in binary -- but round(0.26319,2) would be approximately 0.26 and exactly 0.2600000000000000088817841970012523233890533447265625 decimal.)
The internal value is different from how the values are displayed.
If format short is in effect, then a complicated set of rules is used. Values with absolute value between 0.001 and 999.9999 that do not happen to be integers are generally displayed with 4 digits after the decimal place. Values outside that range are displayed in scientific notation with 5 signficant digits (including the value before the decimal point.) If you are using format short then there is no way to get rid of trailing 0s
If format long g is in effect and the absolute value is between 1e-4 and (1e15 -1e15*eps) then decimal representation will be used, and trailing zeros will be stripped away.
If you want other display rules, then you will need to code them yourself using fprintf() or sprintf() or compose()
Stephen23
on 25 Feb 2024
This question is a very good example of
The actual problem is that the OP was attempting to test for exact equivalence of binary floating point numbers.
The actual solution is given here:
Answers (4)
Azzi Abdelmalek
on 22 Jun 2013
Edited: Azzi Abdelmalek
on 22 Jun 2013
When you use x for calculation use
out = str2num(sprintf('%.3f',x)) % when x is not displayed, the 0 does not appear!
%or
x = round(x*1000)/1000
When you want to display it use
sprintf('%.3f',x)
6 Comments
Walter Roberson
on 25 Jun 2013
Not using regexp, No. Instead use
find( (x(:,1)-b) < 1/1000 )
Steven Lord
on 23 Feb 2017
This is an old discussion, but if you're using release R2014b or later you can round to a specified number of digits.
2 Comments
Faez Alkadi
on 21 Sep 2017
Steven, I'm using R2017a. and i used round function as
x=2.123456789123456789
y=round(x,3),
so i get
y=2.123000000000000000
where i want it to be (saved) as
y=2.123
its odd that mat lab doesn't have this. Just dumb whatever comes after certain decimal number.
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 uses binary mantissas cannot exactly represent 1/10, just like 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.
Walter Roberson
on 22 Jun 2013
At the command prompt give the command
format short g
Note: it is not possible to represent 1.123 exactly as a binary double precision number. The closest representable number is 1.1229999999999999982236431605997495353221893310546875
4 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!