필터 지우기
필터 지우기

how to round number in table to two digit number

조회 수: 124 (최근 30일)
Maryam Abdirad
Maryam Abdirad 2017년 6월 29일
댓글: Walter Roberson 2024년 4월 18일
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
Jonathan
Jonathan 2024년 4월 18일
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
Walter Roberson 2024년 4월 18일
fprintf('%10.2f %10.2f\n', [f(:).'; u(:).']);

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 6월 30일
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
Walter Roberson
Walter Roberson 2017년 7월 1일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

JohnGalt
JohnGalt 2018년 6월 6일
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 2017년 6월 29일
편집: John BG 2017년 6월 30일
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 2019년 10월 15일
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
Roberto Osorio
Roberto Osorio 2019년 10월 17일
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 2019년 10월 17일
Unfortunately matlab does not have the capability of user specified default format for disp and tables (and uitable)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by