How to convert number to string with fixed length?

74 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by