Effacer les filtres
Effacer les filtres

round floating numbers in matlab

5 vues (au cours des 30 derniers jours)
Ashraf
Ashraf le 21 Juil 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 commentaires
Jan
Jan le 21 Juil 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 le 22 Juil 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.

Connectez-vous pour commenter.

Réponses (1)

Andrei Bobrov
Andrei Bobrov le 21 Juil 2012
x = .231;
round(x*10)/10;

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by