在 javascript 中用逗号分隔数字(价格)
separate number(price) with comma in javascript
对于产品的价格,我有这种格式:1200000。
我想将格式更改为:1,200,000。
示例:
var price = 12000;
// the price become this:
var price1 = 12,000;
插入逗号必须在数字末尾开始。
您可以使用 toLocaleString
方法根据用户区域设置或您指定的区域设置格式化数字:
var price = 1200000;
console.log(price.toLocaleString()); //formats to the user's locale
console.log(price.toLocaleString('es-ES') //format as Spanish
对于产品的价格,我有这种格式:1200000。
我想将格式更改为:1,200,000。
示例:
var price = 12000;
// the price become this:
var price1 = 12,000;
插入逗号必须在数字末尾开始。
您可以使用 toLocaleString
方法根据用户区域设置或您指定的区域设置格式化数字:
var price = 1200000;
console.log(price.toLocaleString()); //formats to the user's locale
console.log(price.toLocaleString('es-ES') //format as Spanish