Skip to content

Instantly share code, notes, and snippets.

@supermarsx
Created September 10, 2020 12:29
Show Gist options
  • Save supermarsx/e06150dc978b499782a8df6a2c758374 to your computer and use it in GitHub Desktop.
Save supermarsx/e06150dc978b499782a8df6a2c758374 to your computer and use it in GitHub Desktop.
Pads an integer with leading zeros
/*
padInteger
Pads an integer with leading zeros
parameters
n (integer) - Integer to be padded
width (integer) - Padding width
z (string) - Character to pad integer with, default is '0'
*/
function padInteger(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment