Truncating fractions
Show older comments
If A I have like this
>> A=[0.0001 -0.0012 1.0005 0.0040 1.4125]
A =
0.0001 -0.0012 1.0005 0.0040 1.4125
I want to have A values like this
0.00 0.00 1.00 0.00 1.41
How to do in MATLAB ?
Accepted Answer
More Answers (2)
Oleg Komarov
on 7 Apr 2011
Matt's solution is working. Your question isn't specific enough. You want to display, and not to truncate.
A=[0.0001 -0.0012 1.0005 0.0040 1.4125]
sprintf('%4.2f ',A)
Oleg
2 Comments
Raviteja
on 7 Apr 2011
Walter Roberson
on 7 Apr 2011
Raviteja, it is impossible for binary value representation in *any* programming language to store 1.41 _exactly_.
Walter Roberson
on 7 Apr 2011
What you would like to do cannot be done in any finite binary number representation system. 1/10 is an infinitely repeating number in binary, just the same way that 1/7 is an infinitely repeating number in decimal.
You can represent A to two decimal places as character strings for display purposes, but you will not be able to truncate to two decimal places numerically in binary.
>> sprintf('%.53g', 1.41)
ans =
1.4099999999999999200639422269887290894985198974609375
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!