skip to Main Content

Is it possible to find a number of decimal digits for currency (e.g. 2 for USD, 0 for yen) in Javascript?

3

Answers


  1. The number of decimal positions can be extracted using Intl.NumberFormat#formatToParts(). It returns an array of formatting.

    const numberFormatUSD = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
    const numberFormatEUR = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'EUR' });
    const numberFormatJPY = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'JPY' });
    
    const num = 1234.567;
    
    console.log("--- USD ---");
    console.log(numberFormatUSD.formatToParts(num));
    console.log("--- EUR ---");
    console.log(numberFormatEUR.formatToParts(num));
    console.log("--- JPY ---");
    console.log(numberFormatJPY.formatToParts(num));
    .as-console-wrapper { max-height: 100% !important; }

    This can then be programmatically parsed to find type: "fraction" and retrieve its length. If no such part is found, then the length is zero:

    function findLengthOfFraction(currency) {
      const numberFormatUSD = new Intl.NumberFormat('en-US', { style: 'currency', currency });
      return numberFormatUSD.formatToParts(1)
        .find(part => part.type === "fraction")
        ?.value.length ?? 0;
    }
    
    console.log("USD:", findLengthOfFraction("USD"));
    console.log("EUR:", findLengthOfFraction("EUR"));
    console.log("JPY:", findLengthOfFraction("JPY"));
    .as-console-wrapper { max-height: 100% !important; }
    Login or Signup to reply.
  2. Depending on your applications here’s what I came up with. You might find more optimized solutions later.

    I don’t know what format your number is in; more specifically if it is an actual number (123) or a string of a number ("123"), so I supported both.

    function getDigitPlaces(input) {
        var transInput = parseFloat(input);
        
        if (transInput % 1 === 0) {
            return 0;
        } else {
            return String(transInput).split(".")[1].length;
        }
    }

    The transInput variable takes the input to the function and converts it into a number if needed.

    The first if statement checks to see if the converted number is an integer (a number without a decimal) as integers don’t have a decimal to check for. I did this by evaluating the number modulo 1, which is fancy math jargon for taking everything before the decimal out of the number. If this value is zero, the number did not have anything after the decimal, or no decimal at all.

    If the aforementioned condition is false, the function knows there is a decimal, and will isolate the numbers after the decimal by converting the number back to a string, and splitting the string at the decimal point. All that is left is to find the length of the string with the numbers after the decimal.

    I hope this helped!

    Login or Signup to reply.
  3. Yes, you can find the number of decimal digits for a currency value in JavaScript.

    This function takes a currency code as input and returns the corresponding number of decimal digits for that currency.

    function getDecimalDigitsForCurrency(currencyCode) {
      const currencyInfo = {
        USD: 2, // United States Dollar
        EUR: 2, // Euro
        JPY: 0, // Japanese Yen
      };
      return currencyInfo[currencyCode] || 2;
    }
    
    const currencyCode = 'USD';
    const decimalDigits = getDecimalDigitsForCurrency(currencyCode);
    console.log(`Currency ${currencyCode} has ${decimalDigits} decimal digits.`);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search