checking and generating fix length value

조회 수: 2 (최근 30일)
Raza
Raza 2014년 12월 19일
편집: Stephen23 2014년 12월 19일
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
Stephen23
Stephen23 2014년 12월 19일
Are you converting the numeric to string or displaying the numbers somehow?
The numeric formats used to store numbers on computers don't (usually) distinguish between 0834 and 834, so the question does not really make sense unless you are converting the number to a string or displaying it in some other way. Please edit your question to indicate what exactly what you are trying to achieve.
Raza
Raza 2014년 12월 19일
yes i am converting numeric to string.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2014년 12월 19일
편집: Stephen23 2014년 12월 19일
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)

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 19일
a=83
b=num2str(a)
out=[repmat('0',1,4-numel(b)) b]

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by