checking and generating fix length value

1 view (last 30 days)
Raza
Raza on 19 Dec 2014
Edited: Stephen on 19 Dec 2014
i have coed which gives result by summing all the numbers but i need the sum must be of length of 4 numbers
example i add 16 different numbers and sum = 834
i need this 834 in 0843 form???
  2 Comments
Raza
Raza on 19 Dec 2014
yes i am converting numeric to string.

Sign in to comment.

Accepted Answer

Stephen
Stephen on 19 Dec 2014
Edited: Stephen on 19 Dec 2014
Based on your answer to my question above, then you can use sprintf to exactly control with string length and leading zeros:
sprintf('%04d',834)

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 19 Dec 2014
a=83
b=num2str(a)
out=[repmat('0',1,4-numel(b)) b]

Community Treasure Hunt

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

Start Hunting!

Translated by