Created
September 10, 2020 12:29
-
-
Save supermarsx/e06150dc978b499782a8df6a2c758374 to your computer and use it in GitHub Desktop.
Pads an integer with leading zeros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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