Borrar filtros
Borrar filtros

How do I round off exponential numbers?

7 visualizaciones (últimos 30 días)
Nitin Samuel
Nitin Samuel el 17 de En. de 2013
Comentada: Walter Roberson el 16 de Nov. de 2015
I have a code for which I get a long exponential number eg. a = 2.572920056e-3 as the output. I need to round off only the non-exponential part i.e I want the output to be something like 2.573e-3.
I know how to round off integers using ceil, round etc, but how can one round off only the number outside the exponential like mentioned above. Also, the output varies so I cannot use a command with a fixed number like
(ceil(a*10^6)/10^6).
(The fixed number being '6' in this case).

Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de En. de 2013
Editada: Walter Roberson el 17 de En. de 2013
ex = 10^(3-floor(log10(a)));
ar = round(a * ex) / ex;
Caution: might not work for 0 or infinities
  6 comentarios
Mikhail Lisakov
Mikhail Lisakov el 16 de Nov. de 2015
Editada: Walter Roberson el 16 de Nov. de 2015
Solution is great but fails for powers > 0 (though I've used it to round significand to integer).
To account for possible powers greater than 0 there should be a condition like this :
if(ex < 0)
ar = round(a * ex) / ex;
} else {
ar = round(a / ex) * ex;
}
Walter Roberson
Walter Roberson el 16 de Nov. de 2015
Different requirement, Mikhail. That formula would be for rounding decimals after the decimal point in fixed point format, but the original question was for rounding in engineering format.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by