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.

 采纳的回答

Matt J
Matt J 2022-12-2
编辑:Matt J 2022-12-2
x=0.147;
floor(x*100)/100
ans = 0.1400

1 个评论

Thank you for your answer, it solved my meshing problem.

请先登录,再进行评论。

更多回答(2 个)

Example:
X = 0.143;
Y = sprintf('%.2f',X);
Y = str2double(Y)
Y = 0.1400
Someone ahd same question before. Next time, try to Google your question before asking.

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.
Y = 0.1500
The answers from Matt J and Les Beckham are correct.
True, I just reffered to someone else's answer.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Elementary Math 的更多信息

产品

版本

R2021b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by