필터 지우기
필터 지우기

how to get only 3 digit under point

조회 수: 59 (최근 30일)
octopus_love
octopus_love 2017년 9월 11일
댓글: Walter Roberson 2017년 9월 11일
Hello,
I want to get the only 3 digit under the point. For example,
a = 123.4567;
and I want to get just modify_a = 123.456. I tried the code
a = round(a*10^3)/10^3
but the result is , a = 123.457 0;
How can I remove the '0' point? please help me.
Thank you in advance.
  댓글 수: 2
octopus_love
octopus_love 2017년 9월 11일
Thank you everyone. This was intended to display a specific value on GUI.
I applied the
a = 123.4567
var_a = round(a, 3);
and tried to
msgbox(num2str(var_a))
and it was displayed the '123.456'. I overlooked about the data type.
Thank you.
Walter Roberson
Walter Roberson 2017년 9월 11일
msgbox( sprintf('%.3f', var_a) )
provided that rounding is acceptable.
Note: round(a,3) does rounding, not truncation. 123.4567 would round to 123.457

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

채택된 답변

Image Analyst
Image Analyst 2017년 9월 11일
편집: Image Analyst 2017년 9월 11일
Use round() and pass in the number of digits as the second input:
a3 = round(a, 3)
Keep in mind that it's still a 64 bit number but it will be rounded and all of the umpteen digits after the third decimal place will be 0. They will still be there (you can't get rid of them), but they will be zero. You can then print out that number with fprintf() or sprintf() with 1,2,3,4,5 or however many decimal places you want, and you will see however many you told it to print, just keep in mind that any digits after 3 will be 0.
fprintf('%.3f', a); % Shows n.nnn
fprintf('%.6f', a); % Shows n.nnn000
fprintf('%.1f', a); % Shows n.n
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 9월 11일
... well, except for the fact that the fraction 0.456 is not exactly representable in IEEE 754 Double Precision. The closest representable number is 123.4560000000000030695446184836328029632568359375

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 9월 11일
In order to do that you will need to override the important internal routine toolbox/matlab/lang/@double/display to implement an additional "format" that has exactly three decimal places and which truncates. I would expect that would be a notable amount of work to get right without interfering with any of the current uses of the routine.
The built in "format" can display 2 digits after the decimal place ("format bank" or sometimes "format short g") or 4 digits after the decimal place ("format short" or "format short eng"), or 15 digits after the decimal place ("format long"), or up to 15 digits after the decimal place, stopping the remaining digits of the 15 are all 0. The "format" routines all round.
For any other format like 3 digits exactly with truncation, you need to override the display method. Or you could do the display formatting yourself, using sprintf() or fprintf()

카테고리

Help CenterFile Exchange에서 선 플롯에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!