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 ビュー (過去 30 日間)
古いコメントを表示
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?
採用された回答
Walter Roberson
2018 年 7 月 28 日
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 件のコメント
Walter Roberson
2018 年 7 月 28 日
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
その他の回答 (1 件)
Star Strider
2018 年 7 月 28 日
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.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!