round floating numbers in matlab

8 次查看(过去 30 天)
Ashraf
Ashraf 2012-7-21
Hi everybody,
I have a simple question in matlab, but I don't have an answer. maybe somone help me.
if I have a floating number, like : 0.231, how can I "round" them to the nearest floating number, like: 0.200? I want only one digit after the floating point...
Thanks,
  2 个评论
Jan
Jan 2012-7-21
As usual a warning: 0.199999999999999 can be the nearest floating point number also, because not all decimal number have an exact binary representation. See: 0.3 - 0.2 ~= 0.1
Walter Roberson
Walter Roberson 2012-7-22
The situation is worse than "not all decimal number have an exact binary representation": the only decimal numbers (with at least one digit after the decimal point) that can have binary representation, are the ones whose last non-zero digit is a "5". For example, 0.125, .25, .375, .5 have exact binary representations, but 0.124 and 0.126 cannot have exact binary representation. Not everything that ends with a "5" does have exact binary representation; (e.g., 0.35 does not), but the ones that do not end in "5" never do.

请先登录,再进行评论。

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2012-7-21
x = .231;
round(x*10)/10;

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by