how to round number in table to two digit number

239 views (last 30 days)
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
  4 Comments
Jonathan
Jonathan on 18 Apr 2024 at 19:07
did you ever get an asnwer to this? ive read below but cannot find the solution. i know of fprintf(.... buti dont know how to format the inside of the brackets to show whjat i need (2dp)

Sign in to comment.

Accepted Answer

Walter Roberson
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.
  1 Comment
Walter Roberson
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.

Sign in to comment.

More Answers (3)

JohnGalt
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

John BG
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.
.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG

Roberto Osorio
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.
  3 Comments
Roberto Osorio
Roberto Osorio on 17 Oct 2019
I don't mean the internal representation (which you change with the round function), but the display. Using sprintf or fprintf does the trick for a scalar or a row vector.
sprintf('%.2f %.2f',[2.99792 pi])
ans =
'3.00 3.14'
Of course here the result is a char array. What I really would like is a generalization of 'format bank' for an arbitrary number of digits (other than 2) after the decimal dot.
>> format bank
>> [2.99792 pi]
ans =
3.00 3.14
>> array2table([2.99792 pi])
ans =
1×2 table
Var1 Var2
____ ____
3.00 3.14
Walter Roberson
Walter Roberson on 17 Oct 2019
Unfortunately matlab does not have the capability of user specified default format for disp and tables (and uitable)

Sign in to comment.

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!