首页 > 解决方案 > 在javascript中用逗号分隔数字(价格)

问题描述

对于产品的价格,我有这种格式:1200000。

我希望将格式更改为:1,200,000。

这个例子 :

var price = 12000;
// the price become this: 
var price1 = 12,000;

插入逗号必须从数字末尾开始。

标签: javascriptstringcomma

解决方案


您可以toLocaleString根据用户语言环境或您的规范的语言环境使用方法格式编号:

var price = 1200000;
console.log(price.toLocaleString()); //formats to the user's locale
console.log(price.toLocaleString('es-ES') //format as Spanish


推荐阅读