How to change the field width of an exponential notation?

조회 수: 5 (최근 30일)
Oscar Espinosa
Oscar Espinosa 2020년 5월 4일
편집: Stephen23 2020년 5월 7일
Hi, Im trying to change the field width and the number of digits of the exponential, I will like to looks like enginnering notation:
3.45866636216603e+002
and what I get its:
3.45866636216603e+02
The formatSpec have the next form:
%21.14e
So, I dont know what Im doing wrong, its suppose to the total numbers before e its 21, 14 of them are located after the point. But doesnt seem to be like that.
  댓글 수: 1
Stephen23
Stephen23 2020년 5월 4일
편집: Stephen23 2020년 5월 7일
"...its suppose to the total numbers before e its 21"
The fieldwidth does not specify the number of digits, it specifies the total number of printed characters, including leading spaces, leading zeros, the significant digits, the decimal radix, the exponent character, the exponent digits, and any sign characters. All of these are included in the field width. If the number representation is shorter than the fieldwidth the output string will be padded with leading spaces or leading zeros.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 5월 4일
편집: Stephen23 2020년 5월 6일
Given >=2 digits, ensure >=3 digits:
>> val = 3.45866636216603e2;
>> str = sprintf('%.14e',val)
str =
3.45866636216603e+02
>> str = regexprep(sprintf('%.14e',val),'(?<=\D)\d\d$','0$&')
str =
3.45866636216603e+020
Given >=1 digits, ensure >=3 digits:
>> str = regexprep(sprintf('%.14e',val),{'(?<=\D)\d$','(?<=\D)\d\d$'},'0$&')
str =
3.45866636216603e+020
A general solution that can provide any number of digits:
>> num = 3; % required number of digits
>> fun = @(s)sprintf('%0*u',num,sscanf(s,'%u'));
>> str = regexprep(sprintf('%.14e',val),'\d+$','${fun($&)}')
str =
3.45866636216603e+020
  댓글 수: 3
Stephen23
Stephen23 2020년 5월 5일
편집: Stephen23 2020년 5월 6일
Please see my edited answer.
Oscar Espinosa
Oscar Espinosa 2020년 5월 5일
That really work it out. Thank you Stephen.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by