Skip to content

Num-to-string (padded)

ts
function NumToStrPad(num: number, length: number): string;

Returns a string representation of the number num, padded with leading zeroes to ensure the result is at least length characters long. Useful for formatting numbers in dates, filenames, or other contexts where fixed-width numeric strings are required.

Parameters

NameTypeDescription
numnumberThe number to convert and pad.
lengthnumberMinimum length of the resulting string.

Returns

A string representation of num, left-padded with zeroes if necessary to reach the specified length.

NOTE

If the number's string representation is already at least as long as length, no padding is added.

Example

ts
var date = new Date();
var month = date.getMonth() + 1;
var monthStr = NumToStrPad(month, 2);
// For May, monthStr will be "05"