Add extra zeros in front of a number using jQuery?

I have a downloaded file that is formatted like this

MR 1

Mr 2

MR 100

MR 200

MR 300

ETC.

What I need to do is add an extra two 00s before anything before MR 10 and add one extra 0 to MR10-99

So the files are formatted

MR 001

MR 010

MR 076

ETC.

Any help would be great!

+71
javascript
Jun 24 2018-11-11T00:
source share
12 answers

Assuming you have these values ​​stored on some lines, try the following:

function pad (str, max) { str = str.toString(); return str.length < max ? pad("0" + str, max) : str; } pad("3", 3); // => "003" pad("123", 3); // => "123" pad("1234", 3); // => "1234" var test = "MR 2"; var parts = test.split(" "); parts[1] = pad(parts[1], 3); parts.join(" "); // => "MR 002" 
+190
Jun 24 '11 at 9:59 a.m.
source share

I have a potential solution that I believe is relevant, I posted it here:

https://www.facebook.com/antimatterstudios/posts/10150752380719364

basically, you want a minimum length of 2 or 3, you can adjust how much 0 you put in this piece of code

 var d = new Date(); var h = ("0"+d.getHours()).slice(-2); var m = ("0"+d.getMinutes()).slice(-2); var s = ("0"+d.getSeconds()).slice(-2); 

I knew that I always get at least one integer (the reason is hour 1, hour 2), etc., but if you cannot be sure of getting anything other than an empty string, you can simply do "000" + d.getHours () to make sure you get the minimum.

then you want 3 numbers? just use -3 instead of -2 in my code, I just write this because I wanted to build a 24-hour clock in super-light mode.

+32
Jun 26 2018-12-12T00:
source share

Here is a solution that I liked for its simplicity from answering a similar question :

 var n = 123 String('00000' + n).slice(-5); // returns 00123 ('00000' + n).slice(-5); // returns 00123 



UPDATE

As @RWC expected, you can of course beautifully wrap this in a general function like this:

 function leftPad(value, length) { return ('0'.repeat(length) + value).slice(-length); } leftPad(123, 5); // returns 00123 

And for those who do not like a slice:

 function leftPad(value, length) { value = String(value); length = length - value.length; return ('0'.repeat(length) + value) } 

But if performance matters, I recommend reading the related answer before choosing one of the suggested solutions.

+14
Apr 29 '16 at 7:46
source share
 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 (1, 3) = "001" //addLeadingZeros (12, 3) = "012" //addLeadingZeros (123, 3) = "123" 
+7
Jun 24 2018-11-11T00:
source share

This is the function that I usually use in my code to add zeros to a number or string.

The inputs are a string or a number (str) and the desired output length (len).

 var PrependZeros = function (str, len) { if(typeof str === 'number' || Number(str)){ str = str.toString(); return (len - str.length > 0) ? new Array(len + 1 - str.length).join('0') + str: str; } else{ for(var i = 0,spl = str.split(' '); i < spl.length; spl[i] = (Number(spl[i])&& spl[i].length < len)?PrependZeros(spl[i],len):spl[i],str = (i == spl.length -1)?spl.join(' '):str,i++); return str; } 

};

Examples:

 PrependZeros('MR 3',3); // MR 003 PrependZeros('MR 23',3); // MR 023 PrependZeros('MR 123',3); // MR 123 PrependZeros('foo bar 23',3); // foo bar 023 
+4
May 10 '12 at 16:13
source share

If you split the space, you can add leading zeros using a simple function, for example:

 function addZeros(n) { return (n < 10)? '00' + n : (n < 100)? '0' + n : '' + n; } 

So, you can check the length of the string and, if it is less than 6, divide by space, add zeros to the number, and then combine it together.

Or as a regex:

 function addZeros(s) { return s.replace(/ (\d$)/,' 00$1').replace(/ (\d\d)$/,' 0$1'); } 

I am sure that someone can do this with one replacement, and not with two.

Edit - Examples

 alert(addZeros('MR 3')); // MR 003 alert(addZeros('MR 23')); // MR 023 alert(addZeros('MR 123')); // MR 123 alert(addZeros('foo bar 23')); // foo bar 023 

At the end of the line will be placed one or two zeros from a number in a row with a space in front of it. He doesn't care what bit in front of space.

+2
Jun 24 2018-11-11T00:
source share

Just for fun, do it in a long nasty way ....:
(NOTE: ive is not used, and I would not recommend using this.!)

 function pad(str, new_length) { ('00000000000000000000000000000000000000000000000000' + str). substr((50 + str.toString().length) - new_length, new_length) } 
+2
Nov 14 '13 at 15:42
source share

I needed something similar the other day, Pude, and not always 0, I would like to say what I want to lay in front. Here is what I came up with for the code:

 function lpad(n, e, d) { var o = ''; if(typeof(d) === 'undefined'){ d='0'; } if(typeof(e) === 'undefined'){ e=2; } if(n.length < e){ for(var r=0; r < e - n.length; r++){ o += d; } o += n; } else { o=n; } return o; } 

Where n is what you want to fill, e is the power you want to fill (the number of characters must be long), and d is what you want it to be supplemented. It seems to work well for what I need it for, but it will fail if "d" is more than one character, these are some cases.

+1
Jun 23 '14 at 1:12
source share
 var str = "43215"; console.log("Before : \n string :"+str+"\n Length :"+str.length); var max = 9; while(str.length < max ){ str = "0" + str; } console.log("After : \n string :"+str+"\n Length :"+str.length); 

It worked for me! To increase zeros, update the variable < max

Working link url: Adding extra zeros in front of a number using jQuery? :

+1
Jun 02 '15 at 14:39
source share

str can be a number or a string.

 formatting("hi",3); function formatting(str,len) { return ("000000"+str).slice(-len); } 

Add zeros if you need big numbers

+1
Aug 23 '17 at 8:36 on
source share

In simple terms, we can write the following:

 for(var i=1;i<=31;i++) i=(i<10) ? '0'+i : i; 

// Because most of the time we need this for a day, month or amount.

0
Oct 11 '13 at 7:09
source share

Know that this is an old post, but here is another short, effective way: zeros

edit: dur. if num is not a string, you should add:

len -= String(num).length;

else, all is good

 function addLeadingZeros(sNum, len) { len -= sNum.length; while (len--) sNum = '0' + sNum; return sNum; } 
0
Jan 20 '15 at 16:12
source share



All Articles