필터 지우기
필터 지우기

str2double/str2num

조회 수: 1 (최근 30일)
Tsuwei Tan
Tsuwei Tan 2018년 5월 10일
댓글: Walter Roberson 2018년 5월 10일
day='0001'; str2double(day) would give me 1 as answer, how do I get 0001 exact four number of digits for instance?

채택된 답변

James Tursa
James Tursa 2018년 5월 10일
편집: James Tursa 2018년 5월 10일
Floating point variables do not have leading 0's physically stored in memory (not counting the denormalized numbers of course). So 0001 and 1 are stored exactly the same in memory. If you want to display the leading 0's then you need to use a format that specifies that on print out. E.g.,
>> day = '0001'
day =
0001
>> d = str2double(day)
d =
1
>> fprintf('%04d\n',d)
0001
  댓글 수: 2
Tsuwei Tan
Tsuwei Tan 2018년 5월 10일
Thank you James! it seems that I have to manually check on my code, since I have some strings like '001', '0001', and '00001' that I have to count how many digits before I fprintf them back to number.
Walter Roberson
Walter Roberson 2018년 5월 10일
day = '0001';
nd = length(day);
d = str2double(day);
fprintf('%0*d\n', nd, d);
... which leads one to wonder why you do not just print out day instead of the converted value.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by