Filter löschen
Filter löschen

round floating numbers in matlab

8 Ansichten (letzte 30 Tage)
Ashraf
Ashraf am 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,
  2 Kommentare
Jan
Jan am 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
Walter Roberson am 22 Jul. 2012
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 21 Jul. 2012
x = .231;
round(x*10)/10;

Community Treasure Hunt

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

Start Hunting!

Translated by