Formating String Parameter Left of Decimal
조회 수: 1 (최근 30일)
이전 댓글 표시
In the following code, I want to create the str variable to read: Radius=100E-6 meter
However, I keep getting: Radius=1.00E-4 meter
I can't seem to force it to place more digits to the left of the decimal. How do I revise sprintf to produce the former instead of the latter?
a=100E-6;
str=sprintf('Radius=%3.0E meter,',a);
I know the answer is probably something simple, but I just can't seem to figure it out.
Thanks in advance,
M Ridzon
댓글 수: 2
Stephen23
2017년 9월 25일
The standard C-based MATLAB command sprintf does not produce "engineering" exponents, it always produces exponents with one digit before the decimal point. You might like to read these (but don't use the first answer!):
Stephen23
2017년 9월 25일
편집: Stephen23
2017년 9월 25일
>> num2sip(1.234e-4)
ans = 123.4 u
Following your example you could do something like this:
>> sprintf('Radius = %smeter',num2sip(1.234e-4,[],true))
ans = Radius = 123.4 micrometer
Note that unlike the accepted answer num2sip correctly returns the requested precision, regardless of the order of the number:
>> num2sip(1.4567e-4)
ans = 145.67 u
>> num2sip(1.4567e-5)
ans = 14.567 u
>> num2sip(1.4567e-6)
ans = 1.4567 u
채택된 답변
Star Strider
2017년 9월 25일
I wrote a little utility function for my own purposes to do this a while ago:
engstr = @(x) [x*10.^(-3*floor(log10(abs(x))/3)) 3*floor(log10(abs(x))/3)]; % Engineering Notation Mantissa & Exponent
x = 1E-4;
Result = sprintf('%3.0fE%-.0f', engstr(x))
Result =
'100E-6'
Configure the sprintf format string to produce the output you want.
댓글 수: 3
추가 답변 (1개)
John D'Errico
2017년 9월 25일
Personally, 100e-6 seems like a silly and confusing way to write the number 1e-4. And at some point in my educational career, I even spent some time as an engineer. What you are probably looking for is known as engineering notation.
format SHORTENG
a = 1e-4
a =
100.0000e-006
So can you just display the number directly? I don't see a simple way to force sprintf to work, as it does not seem to include the engineering notation form as an option.
댓글 수: 2
John D'Errico
2017년 9월 25일
Personally, I was surprised that sprintf did not offer engineering notation as an option. I suppose you could write a utility that worked like sprintf, but used engineering notation. It would take some amount of effort, that might not be worth the mental energy expended.
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!