Dodaj wiodące zera do numeru JavaScript
const number = 5;
console.log(number.toString().padStart(2, '0')); // expected output: "05"
Smiling Skipper
const number = 5;
console.log(number.toString().padStart(2, '0')); // expected output: "05"
function padLeadingZeros(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
padLeadingZeros(57, 3);// "057"
padLeadingZeros(57, 4); //"0057"