javascript - 在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
推荐阅读
- python - 将 Django 连接到 PostgreSQL - 尽管安装,但加载 psycopg2 模块时出错
- matplotlib - 在 python 3.9 中安装 matplotlib 时出错
- oracle - Internet Explorer 中的特殊字符(点)在 Weblogic 和 Oracle 中未正确保存
- javascript - 如何从用户脚本访问 webpack 模块?
- linux - 磁盘上的可用空间不可用
- azure-functions - 值不能为空。(参数'uriString')
- python - 为每组数据打印一个列表到新行
- snowflake-cloud-data-platform - 将 SQL Server 转换为 Snowflake SQL,如何编写 INSERT 语句以将查询中的数据放入临时表?
- php - 根据所选日期显示期初余额和期末余额 PHP mysqlquery
- r - 如何解释代码“x=numeric(n)”?