# how to round number in table to two digit number

416 views (last 30 days)
Maryam Abdirad on 29 Jun 2017
Commented: Walter Roberson on 17 Oct 2019
I have a table as an output in my code but all of numbers have 5 digit numbers after decimal. How I should change it to two decimals for whole of the table? (like 0.38878 to 0.39)
this is last part of my code
f=(x1(1:21)*AA)';
u=[x1(22:end) 0 0 0]';
Data=table(f,u)
f u
______ _____
4.4532 4e-07
9.4421 4e-07
9.1827 4e-07
2.3218 4e-07
2.1314 4e-07
3.7149 4e-07
5.7272 4e-07
7.6894 4e-07
1.4933 4e-07
13.726 4e-07
##### 2 CommentsShowHide 1 older comment
Maryam Abdirad on 30 Jun 2017
this is last part of my code
f=(x1(1:21)*AA)';
u=[x1(22:end) 0 0 0]';
Data=table(f,u)
Data =
f u
______ _____
4.4532 4e-07
9.4421 4e-07
9.1827 4e-07
2.3218 4e-07
2.1314 4e-07
3.7149 4e-07
5.7272 4e-07
7.6894 4e-07
1.4933 4e-07
13.726 4e-07
9.3519 4e-07
6.3301 4e-07
2.918 4e-07
4.478 4e-07
2.0276 4e-07
2.6431 4e-07
4.6811 4e-07
5 0
10 0
5 0

Walter Roberson on 30 Jun 2017
Data = table( round(f,2), round(u,2))
However, you need to distinguish between what is stored and how it is displayed. MATLAB uses IEEE 754 Double Precision for values like 0.38878 and those are represented by 53 bits of precision in binary. It is not possible to exactly represent 1/10 in binary, so even though you might have rounded to 0.39, the number that will be stored will not be 39/100 and will instead be 0.39000000000000001332267629550187848508358001708984375 which is 3512807709348987/9007199254740992 . You cannot get it to store 39/100 exactly in numeric form except by going symbolic
When you have numeric values stored in a table then what is output for disp() or giving the variable name depends on what your current format is set to. From your description you probably have it set to the default, format short e . If you were to command
format long g
and display the table you would see more decimal places, due to the difference between what the display formatting is and what is stored. If you command
format bank
then that just might work for you, provided that you do not mind if those 4e-07 show up as 0. format bank is two digits exactly after the decimal place.
For any custom display, you need to extract the values and format them yourself.
Walter Roberson on 1 Jul 2017
At least up to R2017a, floor() and ceil() only permit a second parameter when the first parameter is of type duration, in which case a time unit to round to may be given.

JohnGalt on 6 Jun 2018
If it's just a quick cleanup of the display of the numbers, you could just round the data in the table directly e.g.
array = rand(5,10);
tbl = array2table(array);
tbl.Variables = round(tbl.Variables,1)
of course, this actually modifies the data so you might want to create a copy of your table first

Roberto Osorio on 15 Oct 2019
It's a pity that Matlab doesn't distinguish between significant and non-significant trailing zeros. If I round(2.99792) [this is the speed of light in units of 1e8 m/s] to 3 digits, I should get 3.00 (the two zeros are significant), not 3e8. The latter should be displayed only when you round to 1 digit.
Walter Roberson on 17 Oct 2019
Unfortunately matlab does not have the capability of user specified default format for disp and tables (and uitable)

John BG on 29 Jun 2017
Edited: John BG on 30 Jun 2017
Hi Maryam
for each element of the table do the following
pull up 2 decimals, then apply the rounding ceiling flooring as preferred and bring down 2 least significant digits back to decimals
A=3.1416
floor(A*100)/100
= 3.14
or
ceil(A*100)/100
= 3.15
there's also the command round , it depends on how you want to approximate that you may want to chose one of these 3 commands.
.
John BG