Rounding a decimal down
14 次查看(过去 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!