Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想做这个问题的确切内容,但不必给出固定数量的左侧零,而是能够给出该数字总共必须具有的固定位数。
例如,如果我有 number32并且长度为4,则结果应该是:
32
4
0032
但是如果相反我有 number 122,那么只0应该填充一个:
122
0
0122
关于如何在 Matlab 上有效地实现这一点的任何想法?sprintf?
sprintf
这将留下最多 4 个 0
sprintf('%04d', some_value);
您可以在 in和 in 中使用字符串格式:num2strsprintf
num2str
num2str( 122, '%04d' );
或者
sprintf( '%04d', 32 );