I have a number in Javascript, that I know is less than 10000 and also non-negative. I want to display it as a four-digit number, with leading zeroes. Is there anything more e
Since ES2017 padding to a minimum length can be done simply with String.prototype.padStart and String.prototype.padEnd:
let num = 3
let str = num.toString().padStart(3, "0")
console.log(str) // "003"
Or if only the whole part of a float should be a fixed length:
let num = 3.141
let arr = num.toString().split(".")
arr[0] = arr[0].padStart(3, "0")
let str = arr.join(".")
console.log(str) // "003.141"