Skip to content

@technobuddha > library > Math > Verbalization

Function: pad()

ts
function pad(input: number, length: number): string;

Defined in: pad.ts:17

Add leading zeros to a number to ensure a string of a minimum length

Parameters

ParameterTypeDefault valueDescription
inputnumberundefinedThe number to pad
lengthnumber2The minimum length of the resulting string

Returns

string

number as a string with leading zeros as needed

Example

typescript
pad(5); // "05"
pad(42, 4); // "0042"
pad(-7, 3); // "-07"
pad(NaN, 4); // " NaN"
pad(Infinity, 6); // "Infinity"