Pad number

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

Pads a given number to the specified length.

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.