How to use Intl.NumberFormat object In GIT

The Intl.NumberFormat object enables language-sensitive number formatting. We can use it to format numbers with leading zeros by specifying a minimum integer digit count. This approach is beneficial because it can handle various localization requirements and ensures consistent formatting across different locales.

Syntax:

let formatter = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 2,
useGrouping: false
});
let prepended_number = formatter.format(number);

Example: In this example, we are using the Intl.NumberFormat object to format the numbers.

JavaScript
function prependNumber() {
    let single_digit = 1;
    let two_digits = 3;

    let formatter = new Intl.NumberFormat('en-US', {
        minimumIntegerDigits: 2,
        useGrouping: false
    });

    let prepended_out = formatter.format(single_digit);
    let prepended_out2 = formatter.format(two_digits);

    console.log(prepended_out);
    console.log(prepended_out2);
}

prependNumber();

Output
01
03




How to format numbers by prepending 0 to single-digit numbers ?

We will be having a number and we need to check whether the given number has prepending 0 or not. If a number does not acquire prepending 0 then we will add that 0 with the help of some methods. If 0 is present then we will print that number the same as it is.

A number can be formatted to prepend a 0 to single-digit numbers using 3 approaches: 

Table of Content

  • Method 1: Using padStart() method
  • Method 2: Checking if number is less than 9
  • Method 3: Using the slice() method
  • Method 4: Using Intl.NumberFormat object

Similar Reads

Method 1: Using padStart() method

The padStart() method is used to pad a string with another string to a certain length. The padding is started from the left of the string. It takes two parameters, the target length, and the string to be replaced. The number to be formatted is first converted to a string by passing it to the String constructor. The padStart() method is used on this string with the length parameter given as 2 and the string to be replaced with, given the character ‘0’. This will format any single-digit number to 2 digits by prepending a ‘0’ and leaving 2-digit numbers as is....

Method 2: Checking if number is less than 9

In this method, the number is first checked if it is less than 9. If true, the character ‘0’ is appended to the number otherwise, the number is returned without any change. This will format any single digit number to 2 digits by prepending a ‘0’ and leave 2 digit numbers as is....

Method 3: Using the slice() method

The slice() method is used to extract parts of a string from the specified starting and ending indices. First, the number is prepended with a ‘0’ character regardless of it being a single digit. This will make the single digit number into 2 digits but the 2-digit number would be converted to a 3-digit one with the extra ‘0’. The slice() method is used to extract the last 2 digits of the resulting number. This will correctly get the last 2 digits of the 2-digit number discarding the extra ‘0’ added to it. The single-digit number is now formatted with a ‘0’....

Method 4: Using Intl.NumberFormat object

The Intl.NumberFormat object enables language-sensitive number formatting. We can use it to format numbers with leading zeros by specifying a minimum integer digit count. This approach is beneficial because it can handle various localization requirements and ensures consistent formatting across different locales....