Skip to content

Pad a number to two digits with Javascript

Similar to padding a number with leading zeros, we can pad a number into two digits in JavaScript.

Advertisements
function padToTwoDigits(number) {
     return (number < 10 ? '0' : '') + number
}

console.log(padToTwoDigits(9)); //09
console.log(padToTwoDigits(19)); //19
See also  Most Common Word - Leetcode Challenge - Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.