Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
How to format currency symbol in Intl.NumberFormat?

In a JSON data, I've certain currency data values in the form as follows:

const currencyData = [
    {
      "₹": "INR"
    },
    {
      "¥": "JPY"
    },
    {
      "€": "EUR"
    },
    {
      "£": "GBP"
    }
  ];

I've created a function which accepts 2 parameters one for 'amount' and other for 'currency code', but, what could be done, when second parameter is "$" or "¥" i.e a currency symbol. My intention is to map the currency symbol with above JSON data and give the relevant output, like for e.g: "$" is passed then it should filter comparing with the above JSON data and pass "USD" output and then it can be passed in the below function

const currencyFormatterNew = (amount, currCode) => {
    let currencyCode;
    try {
      if (currCode == "undefined" || currCode == "") {
        currencyCode = "";
      } else {
        currencyCode = currCode;
        return new Intl.NumberFormat("en-US", {
          style: "currency",
          currency: currencyCode,
          minimumFractionDigits: 0
        }).format(amount);
      }
    } catch (err) {
      console.log(err);
    }
  };

The function is called as following: {currencyFormatterNew(10000, "JPY")}

What would be optimal solution to map the 'currency symbol' with above json data and then give required output which could be used in the function created?

Here is the codesandbox link: https://codesandbox.io/s/selection-and-operation-antd-4-17-0-alpha-0-forked-2sqzt

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

  const getCurrencyCode = (code) => {
    const k = currencyData.find(a => Object.keys(a) == code);
    return Object.entries(k)[0][1];
  }  

you can try this. If you want to you can change the last return statement as following

 return Object.values(k);  // it will return an array
about 4 years ago · Juan Pablo Isaza Relatório

0

After the JSON has been loaded, store the currencyData array into an object:

const currencyMap = Object.assign({}, ...currencyData)

Then you can just use property accessor [prop] to get the equivalent symbol. This will be better for performance since you don't need to call .find or similar every time your function is called because those functions does a loop.

const currencyData = [
    {
      "₹": "INR"
    },
    {
      "¥": "JPY"
    },
    {
      "€": "EUR"
    },
    {
      "£": "GBP"
    }
];

const currencyMap = Object.assign({}, ...currencyData)
console.log(currencyMap);

let symbol = '₹';
console.log(currencyMap[symbol]);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda