How to reduce precision of digit after decimal point? Matlab (R2020a)

if i have
a = 1.2345 or
a = 22.34325 or
a = 334.659809832
i want to reduce precision after decimal point to two digit so results should be
a= 1.23
a = 22.34
a = 334.65
i have tried ceil,floor, vpa, round(a,2) but this doesnt give me the above required result... i have searched the matlab answer mostly old questions replied with "fprintf which may support older versions. Kidly Guide

 Accepted Answer

If you are only concerned with displaying the results with two digits after the decimal, then you can use fprintf
a = 334.659809832
fprintf('a=%.2f\n', a)

13 Comments

Thanks Ameer, its working fine
Dear Ameer i have a request, can u also please share an alternate method for older version....because its working in R2020a but not in R2019a.
should be working even for very old releases
tested successfully on a R12 release (back in 2001 !)
Ok Mathieu, i will try again... Thanks for ur fast Reply..
Basically Yes its working on any version but to some extent only... when we use
a = 334.659809832
fprintf('a=%.2f\n', a)
its display use the desired result >>>> a = 334.65
but the issue is when we again display vairiable 'a'... it shows
a = 334.659809832
means Variable 'a' is not updated in the workspace.. i also tried
b = a
its gives
b = 334.659809832
Kindly guide how to resolve this because i want to use further the updated variable 'a', because instead of only Displaying i m also want to use it in my code.
Ok, do you want to change the display style of the variable 'a' permanently. That is not possible in MATLAB. You need to use fprintf() every time you need to display it.
The other option is to declare your own class (probably a subclass of double) and overload the disp() function. However, I wouldn't recommend it because it will unnecessarily make your code complicated and heavily affect code efficiency.
Dear Ameer Thanks for considering my Querry.. actually i have designed a appdesigner app and it contain some UITable... after importing data and performing several functionalities i displayed them to UITable but i want to reduce the precision of number displayed on UITABle... so if there is any alternate solution kindly share.. this will be a great favour..
achieved desired result via this method but the desired Result has type 'symbolic' when i again convert it into ''double'' it again show increased precision..
Matlab should add a simple way to reduce precison and a option to reuse the Result instead of only displaying..
Now Result = 42.34 is what i want but unable to understand how to extract this value :-((((((
This is for symbolic calculations and cannot be directly used with uitable. One option is to convert your matrix into a string with the required format and then use it to fill the table. For example
x = rand(3);
x_str = compose('%.2f', x);
uit = uitable();
uit.Data = x_str;
Ok Thanks i will try this and Update U soon...
I m Very Gratefull to You Ameer i was struggling to do this for many weeks....Finally with your Guidence i have achieved the desired Goal... This is what i wanted...More than Thanks for ur Help and Support...

Sign in to comment.

More Answers (1)

hi
dirty trick , example to round to 2 digits after decimal :
aa = 0.01*round(a*100)
example :
0.01*round(pi*100)
ans =
3.1400
or when you convert to string :
disp(num2str(pi,3))
3.14

Products

Tags

Community Treasure Hunt

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

Start Hunting!