필터 지우기
필터 지우기

how can I change the number of digits before decimal point?

조회 수: 39 (최근 30일)
Riyadh Muttaleb
Riyadh Muttaleb 2018년 9월 10일
댓글: Steven Lord 2022년 4월 19일
Hi everyone,
I would like to change the number of digits before the decimal point, for example 1.45237E+6 to 145.237E+4
Thanks in advance,
Riyadh

채택된 답변

Stephen23
Stephen23 2018년 9월 10일
편집: Stephen23 2018년 9월 11일
Hmm, interesting problem. Here is one solution that lets you specify how many leading digits you want before the decimal point, before converting to char vector/string:
>> N = 1.45237E+6;
>> D = 3; % digits before decimal point
>> T = 6; % total digits
>> K = 10^(2-D+floor(log10(N)));
>> sprintf('%d%.*e',fix(N/K),T-D,mod(N,K))
ans = '145.237e+04'
Try it with more digits:
>> D = 5;
>> T = 6;
>> K = 10^(2-D+floor(log10(N)));
>> sprintf('%d%.*e',fix(N/K),T-D,mod(N,K))
ans = '14523.7e+02'
  댓글 수: 5
Noah Hovde
Noah Hovde 2022년 4월 19일
How about expressing the number 0.21e-6 just as it is? I tried using your code and it converts it to 02.10e-07. I really want it to stay as 0.21e-6 so as to preserve units in the calculation that I am performing. Thanks for your help!
Steven Lord
Steven Lord 2022년 4월 19일
If you want the exponent to be a multiple of 3, use one of the ENG options for the format function.
x = 0.21e-6
x = 2.1000e-07
y = 0.21e-7
y = 2.1000e-08
format shorteng
disp([x; y])
210.0000e-009 21.0000e-009
format longeng
disp([x; y])
210.000000000000e-009 21.0000000000000e-009

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by