How to convert number to string with fixed length?

58 ビュー (過去 30 日間)
Juan Moreno
Juan Moreno 2020 年 12 月 4 日
回答済み: Asad (Mehrzad) Khoddam 2020 年 12 月 4 日
I need to convert an integer into a string with 4 digits; I mean if the number is 4, I need "0004", if the number is 360, I need "0360", and so on. The string function doesn't work for this. I would like to do it without using the if statement. Thank you.

採用された回答

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 12 月 4 日
You can use sprintf
s=sprintf('%05d',124);
disp(s);
%05d means that you have leading zeros and the total length 0f 5

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by