js number padding to number of characters
export function padStart(number, digits = 2, emptyDigit = 0) {
let length = 0;
let n = Math.abs(number);
let absoluteNumber = n;
do {
n /= 10;
length++;
} while (n >= 1);
const prefix = Array(Math.max((digits - length) + 1, 0)).join(emptyDigit);
return number < 0 ? `-${prefix}${absoluteNumber}` : prefix + number;
}
export default padStart;
var n = 123
String("00000" + n).slice(-5); // returns 00123
("00000" + n).slice(-5); // returns 00123
(" " + n).slice(-5); // returns " 123" (with two spaces)
Also in JavaScript:
- javascript convert file to array
- How to use hash in javascript
- coin change problem all combinations javascript
- does json only support ascii
- auto generate component angular
- js regex to find string but not another
- javascript absolute value
- array sorting javascript insertion sort
- stopped typing jquery
- material-ui icon compass
- how to swap two elements in an array javascript
- get the last element of array javascript
- textinput onpress react native
- settimeout in a for loop javascript
- checkbox is checked jquery
- expressjs cheat sheet
- array==null array.length java script
- how to send post request js fetch
- Unable to resolve "react-navigation-stack" react native
- try catch javascript
- NullInjectorError: R3InjectorError HttpClient
- jquery create input hidden
- settimeout jquery
- How to remove the first and the last character of a string