reduce precision of a number
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
David DeTar
on 23 Feb 2017
Try using fprintf instead of sprintf.
Walter Roberson
on 23 Feb 2017
fprintf() sends to the command window output or a file, instead of constructing a string to be returned. The return value from fprintf() is the number of characters printed, not the string. fprintf() and sprintf() have exactly the same format codes.
CYRUS SAFI
on 24 Feb 2024
Moved: Dyuman Joshi
on 25 Feb 2024
any other alternative opetions for rounding
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
Dwight
on 22 Jun 2013
Dwight
on 22 Jun 2013
Azzi Abdelmalek
on 22 Jun 2013
You have to explain, If you want to display a number 0.123, it's not important if it's double or char.
Dwight
on 22 Jun 2013
Walter Roberson
on 25 Jun 2013
Not using regexp, No. Instead use
find( (x(:,1)-b) < 1/1000 )
Iain
on 25 Jun 2013
find( abs(x(:,1)-b) < 1/1000 )
would be better.
Steven Lord
on 23 Feb 2017
2 votes
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
Dwight
on 22 Jun 2013
Walter Roberson
on 22 Jun 2013
Use "format short g" together with the rounding that Azzi shows.
Note that there is a difference between the internal value stored and the printable representation of the internal value. When you display a number using disp() then the printable representation used depends on which "format" command is in effect. The default is "format short" which shows the value rounded to 4 decimal places unless the value is close enough to an integer that it shows it in integer form instead. But that form is not how the value is stored internally, just how it is printed out. "format short g" will show up to 4 decimal places, dropping trailing 0's from the display.
None of the "format" commands allow you to select the number of decimal places to display. If you want a display behavior different than what "format" can give you, you will need to use code such as sprintf() to format the number into a character string and then display the string. Note that this does not affect the internal representation of the number.
Only a small number of decimal fractions can be stored exactly in binary floating point -- e.g., .5, .25, .75, .125, .375, and so on. The integer multiples of negative powers of 2. Anything else, including 0.1, cannot be represented exactly and will use all 53 bits of precision internally, such as that long string of digits I showed above being necessary to represent "as close as practical" to 0.123
Mayank Ghugretkar
on 19 Aug 2019
use
digits(4)
Answer= vpa(1.123456)
Walter Roberson
on 19 Aug 2019
vpa() requires the Symbolic Toolbox
Francis Agbali
on 24 Mar 2019
0 votes
I would try using
format short
1.23400007787666
1 Comment
mohamed kandil
on 15 Jul 2022
format bank
Categories
Find more on Numeric Types 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!