MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# round floating numbers in matlab

Asked by Ashraf on 21 Jul 2012

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,

Jan Simon on 21 Jul 2012

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 on 22 Jul 2012

http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

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.

## Products

No products are associated with this question.

```x = .231;