Effacer les filtres
Effacer les filtres

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 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (1)

Star Strider
Star Strider le 28 Juil 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.

Catégories

En savoir plus sur Mathematics 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