skip to Main Content

Getting Uncaught RangeError: Invalid currency code : â¦

even though the currency code is correct
see my function below

export const toCommaAmount = (origNum) => {
    console.log("inside toCommaAmount")
    let newNum = origNum
    if (origNum !== null) {
        if (typeof origNum == 'string') {
            newNum = Number(origNum);
        }
        const options = {
            style: 'currency',
            currency: 'u20A6'
        }

        const intNum = new Intl.NumberFormat('en-US', options).format(newNum);
        return intNum.toString()
    }

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out thanks

    export const toCommaAmount = (origNum) => {
        console.log("inside toCommaAmount")
        let newNum = origNum
        if (origNum !== null) {
            if (typeof origNum == 'string') {
                newNum = Number(origNum);
            }
            const options = {
                style: 'currency',
                currency: 'NGN',
                currencyDisplay: 'symbol',
                minimumFractionDigits: 0,
                maximumFractionDigits: 0,
                currencySign: 'accounting'
            }
    
            const intNum = new Intl.NumberFormat('en-NG', options).format(newNum);
            const formatted = intNum.replace(/(?<=d)NGN/g, 'u20A6');
            return formatted.toString()
        }
    }
    

  2. If you check the documentation you can see that the value of currency can only be ISO 4217 currency codes.

    So for Nigeria (as far is I’m aware u20A6 is the currency symbol for Naira) you can use:

    const toCommaAmount = (origNum) => {
      console.log("inside toCommaAmount")
      let newNum = origNum
      if (origNum !== null) {
        if (typeof origNum == 'string') {
          newNum = Number(origNum);
        }
        const options = {
          style: 'currency',
          currency: 'NGN'
        }
    
        const intNum = new Intl.NumberFormat('en-US', options).format(newNum);
        return intNum.toString()
      }
    }
    
    console.log(toCommaAmount(12345));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search