Pad number

Isabelle Viktoria Maciohsek · JavaScript, String, Math · Oct 3, 2020

Pads a given number to the specified length.

  • Use String.prototype.padStart() to pad the number to specified length, after converting it to a string.
const padNumber = (n, l) => `${n}`.padStart(l, '0');
padNumber(1234, 6); // '001234'

Recommended snippets

  • Pad string

    JavaScript, String

    Pads a string on both sides with the specified character, if it's shorter than the specified length.

  • Converts a number in bytes to a human-readable string.

  • Random alphanumeric string

    JavaScript, String

    Generates a random string with the specified length.