Number.prototype.padLeft() - mkloubert/jsToolbox GitHub Wiki

Number.prototype.padLeft(len, pad) method

Returns the number as string with a minimum length with padding chars at the beginning.

Syntax

Number.prototype.padLeft(len, pad);

Parameters

Name Type Description
len Number The minimum length of the result string.
pad String [OPTIONAL] The custom padding string to use (is '0' by default).

Result

The padded number as string.

Examples

var n = 5979;

// '00005979'
var res1 = n.padLeft(8);
// '115979'
var res2 = n.padLeft(6, '1');