Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simplest way for leading zero padding in JavaScript
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
(1).pad(3) // => "001"
(10).pad(3) // => "010"
(100).pad(3) // => "100"
@GAZ082

This comment has been minimized.

Copy link

GAZ082 commented Dec 22, 2017

Handy! Thank you!

@Enelar

This comment has been minimized.

Copy link

Enelar commented Mar 4, 2018

You have bug with negative numbers

Number.prototype.pad = function(size) {
  var sign = Math.sign(this) === -1 ? '-' : '';
  return sign + new Array(size).concat([Math.abs(this)]).join('0').slice(-size);
}
@rroossyyiidd

This comment has been minimized.

Copy link

rroossyyiidd commented Mar 8, 2018

Thanks a lot!

@Utshab500

This comment has been minimized.

Copy link

Utshab500 commented Apr 4, 2018

This is very useful. I have one request. Can you please explain a bit the expression s.length < (size || 2) ? I did not understand why 2 is there?

@marcinappgo

This comment has been minimized.

Copy link

marcinappgo commented Apr 7, 2018

@Utshab500 I think 2 is a default size value.

@Fallibilist

This comment has been minimized.

@ColinWa

This comment has been minimized.

Copy link

ColinWa commented Jul 20, 2018

@endel This is awesome.
@Fallibilist, Thanks a million

@david-kominek

This comment has been minimized.

Copy link

david-kominek commented Jul 25, 2018

Note: padStart is not compatible with any version of Internet Explorer

@Paulofsr

This comment has been minimized.

Copy link

Paulofsr commented Jul 26, 2018

Thanks!

@horroona

This comment has been minimized.

Copy link

horroona commented Aug 28, 2018

Thanks a lot
I use padStart() and it works fine

@stratboy

This comment has been minimized.

Copy link

stratboy commented Sep 18, 2018

Thank you, and thank you @Enelar

@tomcarbon

This comment has been minimized.

Copy link

tomcarbon commented Oct 5, 2018

Thank You

@knoxcard

This comment has been minimized.

Copy link

knoxcard commented Oct 9, 2018

padStart() for the win!

@renoirb

This comment has been minimized.

Copy link

renoirb commented Feb 14, 2019

I came around here and noticed something I ought to tell.
Please don't mutate your prototype chain!

Don't do Number.prototype.pad = function(n) { /* ... */ }

Create a function and a module instead.

I'm not a JS Runtime expert, but here is an MDN link about it

@little-einstien

This comment has been minimized.

Copy link

little-einstien commented Feb 26, 2019

superb bro your a warrior !

@imanabu

This comment has been minimized.

Copy link

imanabu commented Jul 26, 2019

     `${n+100}`.substring(1)
@closetothe

This comment has been minimized.

Copy link

closetothe commented Dec 1, 2019

     `${n+100}`.substring(1)

Hahah, amazing!

@SynCap

This comment has been minimized.

Copy link

SynCap commented Jan 19, 2020

@kl3sk: we don't looking for easy ways!

Disadvantages of String.padStart()

  • works only in modern browsers and environments;
  • don't care about negative
  • works only on String primitives not a Number
(-4+'').padStart(7,'0')
"00000-4"

Overengineering way:

/** ES2015+ -- ultra-fast
@param n <Number> what to pad left with
@param z <Number> (zeros :) ) width which pad to
@param s <Number> (sign :) ) char to pad with
*/

const pz = (n, z = 2, c = '0') =>
	(n+'').length < z ? ((['','-'])[+(n<0)] + c.repeat(z) + Math.abs(n)).slice(-1*z) : n + '';

//** v2, works in carton boxes
function pz(n, z, s) {
  var z= z || 2, s = s || '0';
  return (n+'').length < z ? ((['','-'])[+(n<0)] + c.repeat(z) + Math.abs(n)).slice(-1*z) : n + '';
}

Features:

  • cares about negative numbers
  • cares about length -- don't cut just pads
  • works with Numbers
  • v2 works in legacy trash cans even in MS IE 3.1 and Rhino
  • have a short way (defaults)
@GSingh1994

This comment has been minimized.

Copy link

GSingh1994 commented Jan 24, 2020

Thanks a ton!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.