Borrar filtros
Borrar filtros

round floating numbers in matlab

8 visualizaciones (últimos 30 días)
Ashraf
Ashraf el 21 de Jul. de 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 comentarios
Jan
Jan el 21 de Jul. de 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 el 22 de Jul. de 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.

Iniciar sesión para comentar.

Respuestas (1)

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

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by