matlabzero-padding

How to format the integer value with leading zeros in matlab?


I want format integer value by adding leading zeros and display it as string. For example, I have 3 and I want to display it as 003. I want to do it in matlab.


Solution

  • Use sprintf():

    aa = sprintf('%03d', 3); % aa will be 003
    

    Note that aa here is a string. Check out its documentation for more info.