필터 지우기
필터 지우기

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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by