Format the number to two digits
First, install the library in your project by npm:
$ npm install two-digit
Or Yarn:
$ yarn add two-digit
Connect the library to the project with ES6 import:
import twoDigit from 'two-digit';
Or CommonJS:
const twoDigit = require('two-digit');
Next use the library:
const formattedValue = twoDigit(/* value */);
Type | Description |
---|---|
string or number (if a value of a different type is passed, the function returns the value passed) | Value to be formatted |
Example results for different data:
twoDigit(5); // '05'
twoDigit(15); // 15
twoDigit(120); // 120
twoDigit(2.51); // '02.51'
twoDigit(Math.PI); // '03.141592653589793'
twoDigit(-1); // -1
twoDigit('2'); // '02'
twoDigit('04'); // '04'
twoDigit('10'); // '10'
twoDigit('204'); // '204'
twoDigit('2.1.3'); // '2.1.3'
twoDigit(0b111); // '07'
twoDigit(0b1100); // 0b1100 (12 decimal)
twoDigit(0o5); // '05'
twoDigit(0o12); // 0o12 (10 decimal)
twoDigit(0x8); // '08'
twoDigit(0xf2); // 0xf2 (242 decimal)
twoDigit('J'); // 'J'
twoDigit('Hello World!'); // 'Hello World!'
twoDigit({ name: 'John' }); // { name: 'John' }
twoDigit(Infinity); // Infinity
twoDigit(null); // null
This project is licensed under the MIT License ยฉ 2020-present Jakub Biesiada