Rounding a decimal down
古いコメントを表示
With round(x,2) I can round a number to the nearest hundredth, but how do I round down to the nearest hundredth? For example, both 0.143 and 0.147 should become 0.14.
採用された回答
その他の回答 (2 件)
x = [0.143 0.147];
floor(x*100)/100
Vilém Frynta
2022 年 12 月 2 日
編集済み: Vilém Frynta
2022 年 12 月 2 日
Example:
X = 0.143;
Y = sprintf('%.2f',X);
Y = str2double(Y)
2 件のコメント
It is more efficient to keep this in the numeric domain, rather than converting to text and back.
It also does not work, because SPRINTF rounds values to the nearest digit, it does not round down:
X = 0.147;
Y = sprintf('%.2f',X);
Y = str2double(Y) % Nope, definitely not rounded down.
The answers from Matt J and Les Beckham are correct.
Vilém Frynta
2022 年 12 月 3 日
True, I just reffered to someone else's answer.
カテゴリ
ヘルプ センター および File Exchange で Elementary Math についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!