Azzera filtri
Azzera filtri

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 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Walter Roberson
Walter Roberson il 28 Lug 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 Commenti
Franklin Turbeville
Franklin Turbeville il 28 Lug 2018
I was hoping that there would be an easy solution like there is for precision. Thanks for the response.
Walter Roberson
Walter Roberson il 28 Lug 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

Accedi per commentare.

Più risposte (1)

Star Strider
Star Strider il 28 Lug 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.

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by