I need two decimal values without rounding off

조회 수: 16 (최근 30일)
Chandrika Avvaru
Chandrika Avvaru 2018년 7월 22일
댓글: madhan ravi 2018년 7월 24일
Hi, I have a matrix which do contain decimal values.But I need just 2 decimals without rounding off the values.For example I have a value like 0.0194.My result should be 0.01.How can I do that?

답변 (2개)

madhan ravi
madhan ravi 2018년 7월 22일
편집: madhan ravi 2018년 7월 23일
Hi try this:
a=floor(100 * 0.0194)/100
fprintf('%0.2f',a)
For more clarification click the link below:
https://www.mathworks.com/matlabcentral/answers/32169-how-can-i-format-a-percent-using-2-digits-after-the-decimal
Note:floor command was taken from Sir Walter.
I recently figured out a syntax you were looking for. Try this for sure:
format bank
  댓글 수: 6
Image Analyst
Image Analyst 2018년 7월 24일
You mean the question about the first option being fine? Yes of course it would work - it's basically the answer I gave, before you realized your first answer with sprintf() (which gave 0.02) didn't work and changed it. And then Walter added the option of floor, which handles the rounding in a different direction for negative numbers. So, yes fix() and floor() both can work - just depends on what they want. If there are no negative numbers, it doesn't matter and you can use either fix() or floor().
"format bank" just changes how the numbers are displayed but does not actually change the number at all like fix/floor does.
Walter's last sentence is also important to understand, as is this related FAQ: https://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
madhan ravi
madhan ravi 2018년 7월 24일
Thank you for the information sir.

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


Image Analyst
Image Analyst 2018년 7월 22일
Use fix:
v = 0.0194
v2 = fix(100 * v)/100
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 7월 22일
If your value were -0.0194 and you want the answer -0.01 then fix() is the right thing to use. If you wanted -0.02 instead (the 1/100th that is less than or equal to the number) then you would use floor() instead of fix()

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

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by