1

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Markus Hayner
  • 2,869
  • 2
  • 28
  • 60
  • 1
    Why did you expect `£73.60`? The value is `7360`, not `73.60`. – jonrsharpe Mar 20 '19 at 17:50
  • @jonrsharpe the number is returned from mangoPay which is not adding any commas. So the actual value is 73.60 but is returned as 7360 so I want to display accordingly – Markus Hayner Mar 20 '19 at 17:51
  • What do you mean *"not adding any commas"*? It's a *number*. If you're receiving pennies and need to display pounds, you need to divide it. – jonrsharpe Mar 20 '19 at 17:52
  • For anyone who doesn't understand why this is an **excellent question**, read about floats and money: https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency ... Unfortunately, `Intl.NumberFormat()` does not have something like a `minorUnits: true` option. – Dem Pilafian Jun 20 '21 at 03:34

3 Answers3

2

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)
Anand G
  • 3,130
  • 1
  • 22
  • 28
1

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);
Wrokar
  • 963
  • 7
  • 17
0

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);
Bathri Nathan
  • 1,101
  • 2
  • 13
  • 17