Skip to content

padNumber() Helper Method

The padNumber() Helper Method module accepts a number argument and two optional string arguments, and returns a string. The returned string will be "padded" by prefixing the string in the 3rd argument, according to the number of characters defined in the 2nd argument.

Import

To import the padNumber() Helper Method:

javascript
import { padNumber } from '@obewds/vueventus'

Arguments

Returns: String

ArgsTypeStatusDescription
numberNumberRequiredThe number to be padded
outputLengthNumberOptionalThe total number of characters to output (including the number to be padded), where the default value is 2
padCharacterStringOptionalThe maximum (inclusive) integer number to return as a possible random integer

Use Example

javascript
console.log( padNumber(5) )
// returns (string): 05

console.log( padNumber(10) )
// returns (string): 10

console.log( padNumber(10, 3) )
// returns (string): 010

console.log( padNumber(10, 4, '-') )
// returns (string): --10

Module Code

ts
// ./src/helpers/padNumber.ts

export default function (number: number, outputLength = 2, padCharacter = '0'): string {
    return number.toString().padStart(outputLength, padCharacter)
}

Released under the MIT License