0

I'm beginner in JavaScript and I'm rendering a list that contains some products.

The product price format comes as follows: 3800, 4000, 11000, etc.

The way I should show the user is: 38.00, 40.00, 110.00, etc.

But when I use Intl.NumberFormat, the value is in the format: 3.800,00 €, etc.

Can you tell me how to use Intl.NumberFormat correctly to leave the values in the correct format?

Here's my code I put into codesandbox

Thank you in advance.

Tainara
  • 317
  • 1
  • 5
  • 14
  • 1
    So it's in cents instead of euros? Just divide by 100 and then format it – James Whiteley Oct 10 '20 at 19:43
  • 1
    Does this answer your question? [How to format numbers as currency string?](https://stackoverflow.com/questions/149055/how-to-format-numbers-as-currency-string) – Martheen Oct 10 '20 at 19:44
  • Format of string returned by `Intl.NumberFormat` depends on your local settings. – Jsowa Oct 10 '20 at 19:48
  • Hey James, I divided the amount by 100, but it was 38.00 and not 38.00. Do you know how I can change the comma for the period? – Tainara Oct 11 '20 at 08:07

2 Answers2

1
let num = 3400/100;
console.log(num.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}));

will output > 34.00

I notice in your code that you are using 'de-DE' that will always format the currency to use comas , as the decimal marker, if you change your code to use 'en-IN' will use dot . as the decimal marker.

You can read all about it here

Ricardo Sanchez
  • 4,935
  • 11
  • 56
  • 86
  • `if you change your code to use 'en-IN'` good as a hack, but not a solution. May cause some problems in the future. – Jsowa Oct 11 '20 at 09:34
-1

divide the price by 100 and if you want the last two decimal point like 38.00 try this let price = (3800/100).toFixed(2)

Dipankar Maikap
  • 447
  • 4
  • 13
  • Hey Dipankar, I didn't give the downvote. I divided the amount by 100, but it was 38.00 and not 38.00. Do you know how I can change the comma for the period? – Tainara Oct 11 '20 at 08:05
  • 1
    You can have a workaround like this ```let value="88,9827"; let newValue=value.replace(",", "."); console.log(newValue); ``` – Dipankar Maikap Oct 11 '20 at 08:42
  • @ArtuX the author needs help on formating the output of the Intl.NumberFormat method not on how to convert the number – Ricardo Sanchez Oct 11 '20 at 08:49