简体   繁体   中英

How to format currency in JavaScript?

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 intocodesandbox

Thank you in advance.

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

将价格除以 100,如果你想要像38.00这样的最后两个小数点,试试这个let price = (3800/100).toFixed(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM