Borrar filtros
Borrar filtros

How do I change the amount of places before a decimal when a number is printed in scientific notation? (i.e. 10e6 rather than 1e7)

28 visualizaciones (últimos 30 días)
I currently print a number to the command window using something like this:
fprintf('%.2e',1000)
This will print out as:
1.00e+3
What I want it to print out as is:
10.00e+2
How do I do this?

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Jul. de 2018
You would have to write the code for this yourself; MATLAB does not have provision for this.
Question: under what circumstances should there be two leading digits? For example, how should 0 be printed out? How about 5.3 -- should that be formatted as 53.00e-1 ?
  3 comentarios
Franklin Turbeville
Franklin Turbeville el 28 de Jul. de 2018
I was hoping that there would be an easy solution like there is for precision. Thanks for the response.
Walter Roberson
Walter Roberson el 28 de Jul. de 2018
Engineering notation usually goes by 10^multiple of 3, which is not the same thing you were talking about; you had wanted 1000 to come out like 10.0e2 but in engineering notation it would be 1.0e3

Iniciar sesión para comentar.

Más respuestas (1)

Star Strider
Star Strider el 28 de Jul. de 2018
Try this:
a = 1000;
n = 2;
expstr = @(x,n) [x*10.^(-n*floor(log10(abs(x))/n)) n*floor(log10(abs(x))/n)];
Result = sprintf('%.2fE%+04d', expstr(a,n))
Result =
'10.00E+002'
I wrote this little ‘expstr’ utility function to produce the mantissa and exponent for my own use a few years ago. The first argument ‘x’ is the number, and the second ‘n’ is the resolution you want. It returns a (1x2) vector with the mantissa as the first element and the corresponding exponent as the second. If you set n=3, it produces engineering notation.
The ‘Result’ string is simply an sprintf call that you can change to produce the string you want.
Experiment with it.

Categorías

Más información sobre Mathematics 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