Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  84] [ 1]  / answers: 1 / hits: 18625  / 10 Years ago, fri, august 8, 2014, 12:00:00

I have a ten digit number that will always be constant. I want to pad it so, that it will always remove a zero for every extra number added to the number. Can someone please show me an example of how I can do this?



eg. 0000000001



0000000123 
0000011299

More From » javascript

 Answers
100

You can use this function:



function pad (str, max) {
str = str.toString();
return str.length < max ? pad(0 + str, max) : str;
}


Output



pad(123, 10);    // => 0000000123


JSFIDDLE DEMO


[#69862] Wednesday, August 6, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrellhunterm

Total Points: 82
Total Questions: 109
Total Answers: 98

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
terrellhunterm questions
Mon, Aug 31, 20, 00:00, 4 Years ago
Mon, Aug 5, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Mon, Mar 11, 19, 00:00, 5 Years ago
;