Adding zeros before a string

I need your help.

I would like to create a function that adds some zeros in front of a number. The maximum number of digits a common line must have is 6. Here are some examples:

9 -> 000009 14 -> 000014 230 -> 000230 1459 -> 001459 21055 -> 021055 987632 -> 987632 (Do nothing, there already 6 digits) 
+5
source share
8 answers

Edit: my solution was not very high quality.

See @Roberto's answer below, this is the best solution. If the crawler would like to change this to the accepted answer, I can delete this answer.

-1
source

Simple single line solution without any loops

Works with IE5-11, Firefox, Chrome, etc. Assumes integer input.

  function pad(n) { return ("000000" + n).slice(-6); } 

Run snippet to check:

 <html> <body> <input id="stdin" placeholder="enter a number" maxlength="6"><button onclick="test()">Test</button> <textarea id="stdout" style="width:100%;height:20em;padding:1em;"></textarea> <script type="text/javascript"> function pad(n) { return ("000000" + n).slice(-6); } function test() { var n = parseInt( document.getElementById('stdin').value); var e = document.getElementById('stdout').innerHTML += n + ' = ' + pad(n) + '\n'; } </script> </body> </html> 
+10
source

The following will add zero to the string of numbers until the length is 6.

 var s = '9876' while(s.length < 6){ s = '0' + s } alert(s) 
+5
source

Another one., This works with:

  • strings and numbers
  • processes variable length and
  • allows you to select the fill character

code:

 function padZerosToLength (value, minLength, padChar) { var iValLength= value.toString().length; return ((new Array((minLength + 1) - iValLength).join(padChar)) + value); } 

Here are some examples of variable input results:

 padZerosToLength(1, 6, 0); ===> 000001 padZerosToLength(12, 6, 0); ===> 000012 padZerosToLength(123, 6, 0); ===> 000123 padZerosToLength(1234, 6, 0); ===> 001234 padZerosToLength(12345, 6, 0); ===> 012345 padZerosToLength(123456, 6, 0); ===> 123456 

., with variable length:

 padZerosToLength(1, 1, 0); ===> 1 padZerosToLength(1, 2, 0); ===> 01 padZerosToLength(1, 3, 0); ===> 001 padZerosToLength(1, 4, 0); ===> 0001 padZerosToLength(1, 5, 0); ===> 00001 padZerosToLength(1, 6, 0); ===> 000001 

., and with different filling patterns:

 padZerosToLength(1, 6, 0); ===> 000001 padZerosToLength(1, 6, 1); ===> 111111 padZerosToLength(1, 6, "x"); ===> xxxxx1 padZerosToLength(1, 6, "."); ===> .....1 padZerosToLength(1, 6, " "); ===> 1 padZerosToLength(1, 6, "\u25CF"); ===> ●●●●●1 
+3
source

You will need to convert the number to a string. Then I would split this string into an array and add “0” to the beginning of this array until the length is 6. Then attach. Check out this replica or see the code below: http://repl.it/piT

 var num = 14; var lengthOfNum = 6; var numString = num.toString(); var numArray = numString.split(''); var returnString = ''; while (numArray.length < 6) { numArray.unshift('0'); } returnString = numArray.join(''); 
+1
source

A recursive solution just for this:

 function padNumberString(numberString) { if (numberString.length >= 6) { return numberString; } return padNumberString('0' + numberString); } 
0
source

A simple solution would be

  function addLeadingZeros (n, length) { var str = (n > 0 ? n : -n) + ""; var zeros = ""; for (var i = length - str.length; i > 0; i--) zeros += "0"; zeros += str; return n >= 0 ? zeros : "-" + zeros; } //addLeadingZeros (9, 3) = "009" //addLeadingZeros (15, 3) = "015" //addLeadingZeros (333, 3) = "333" 
-1
source

I have been using the SugarJs API for a long time, and their complementary function works fine.

http://sugarjs.com/api/Number/pad

 (9).pad(6, true) --> 000009 (14).pad(6, true) --> 000014 

etc...

-2
source

All Articles