how to round number in table to two digit number
Mostra commenti meno recenti
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 Commenti
dpb
il 29 Giu 2017
'Pends on how you're outputting to where...show us the code that produces the table.
Maryam Abdirad
il 30 Giu 2017
Modificato: Walter Roberson
il 30 Giu 2017
Jonathan
il 18 Apr 2024
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)
Walter Roberson
il 18 Apr 2024
fprintf('%10.2f %10.2f\n', [f(:).'; u(:).']);
Risposta accettata
Più risposte (3)
JohnGalt
il 6 Giu 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
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
il 15 Ott 2019
1 voto
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 Commenti
Walter Roberson
il 17 Ott 2019
No computer language that is based upon IEEE 754 Binary Floating Point keeps track of significant zeros, and no Intel x64 architecture works in decimal. The IBM Z9 series has an option for IEEE 754 Decimal Floating Point, but that does not keep track of significant zeros either.
Roberto Osorio
il 17 Ott 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
il 17 Ott 2019
Unfortunately matlab does not have the capability of user specified default format for disp and tables (and uitable)
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!