Round value ???

4 ビュー (過去 30 日間)
nayomi
nayomi 2011 年 10 月 18 日
Dear all,
I have to round a number to a specified number of decimal points.Anybody can help me??
Thank you

採用された回答

Daniel Shub
Daniel Shub 2011 年 10 月 18 日

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 10 月 18 日
That cannot be done exactly in binary floating point arithmetic. Just the same way that you cannot exactly represent 1/3 in a fixed number of decimal digits, 1/100 cannot be exactly represented in binary digits.
It is possible to display something with two decimal places, such as by using
disp(sprintf('%.2f', 343.945584))
but you cannot store 343.95 exactly in a floating point number: try
sprintf('%.99f\n', 343.95)
and notice that it is not exactly 343.95 that is stored.
If you do not mind that you can only numerically get a close approximation, then use:
round(N*100)/100
  1 件のコメント
Image Analyst
Image Analyst 2011 年 10 月 18 日
Walter, as you know this is a frequently asked question, since you've repeated it bazillions of times. Yet it's not in the FAQ (that I could find anyway). I'm thinking it should be added to section 6 on Math/Algorithms. What do you think? You can add it, or I could if you want.

サインインしてコメントする。


Pritesh Shah
Pritesh Shah 2011 年 10 月 18 日
Also, Please see Cell, fix and round function for the same. If you want decimal point fix,
Also, You can you format function.
Thank you
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 10 月 18 日
I think you mean ceil rather than cell.
The "format" function cannot be used to get a precise number of decimal places, except for "format bank"

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by