str2double/str2num
2 次查看(过去 30 天)
显示 更早的评论
day='0001'; str2double(day) would give me 1 as answer, how do I get 0001 exact four number of digits for instance?
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!