Rounding a decimal down
17 ビュー (過去 30 日間)
古いコメントを表示
その他の回答 (2 件)
Vilém Frynta
2022 年 12 月 2 日
編集済み: Vilém Frynta
2022 年 12 月 2 日
Example:
X = 0.143;
Y = sprintf('%.2f',X);
Y = str2double(Y)
2 件のコメント
Stephen23
2022 年 12 月 3 日
編集済み: Stephen23
2022 年 12 月 3 日
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!