Markus Hayner
Markus Hayner

Reputation: 2959

How to format the currency using Intl.NumberFormat?

I am trying to format the currency as following

const formatter = new Intl.NumberFormat("en-GB", {
    style: "currency",
    currency: "GBP",
    minimumFractionDigits: 2
  });

const moneyFormat = formatter.format(7360);

Expected £73.60 but it returns £7,360.00

Any idea what am I doing wrong?

Upvotes: 3

Views: 4736

Answers (3)

Anand G
Anand G

Reputation: 3200

If the amount is in pence and the result required in GBP, you have to divide the amount by 100, (considering 100 pence = 1 GBP). The minimumFractionDigits adjust the trailing Zeros after decimal point of the currently but does not manipulate the actual value.

You should try below

const formatter = new Intl.NumberFormat("en-GB", {
  style: "currency",
  currency: "GBP",
  minimumFractionDigits: 2,
});

const moneyFormat = formatter.format(7360 / 100);

console.log(moneyFormat)

Upvotes: 2

Bathri Nathan
Bathri Nathan

Reputation: 1267

this code might help you to attain the format without decimal.

const formatter = new Intl.NumberFormat("en-GB", {
    currency: "GBP"
  });

const moneyFormat = formatter.format(7360);

console.log("$",moneyFormat);

Upvotes: -1

Wrokar
Wrokar

Reputation: 963

The format function takes the raw value and will format it to the amount of fraction digits that you specify. The raw number you gave it was 7360, so it is adding two fraction digits to make it 7360.00. The number it needs to be passed is actually 73.60.

If you are working with the currency value as the whole number, you can simply divide by 100 to display the value you are expecting.

const moneyFormat = formatter.format(7360 / 100);

Upvotes: 1

Related Questions