toLocaleString--金钱格式化

toLocaleString--金钱格式化

参数

locales:可选,带有BCP 47语言标记的字符串或字符串数组,用来表示要转为目标语言的类型,具体参考这个 Intl
options:可选,配置属性对象。
style:数字展示样式
style字段值说明decimal用于纯数字格式(默认)currency用于货币格式percent用于百分比格式unit用于单位格式
currency:当 options.style为currency 时,options.currency 用来表示货币单位的类型
currency字段值说明USD使用美元格式(默认)EUR使用欧元格式CNY使用人民币格式

示例

// 1、将数字进行千分位切割展示
var num = 1331231
console.log(num.toLocaleString()) // 1,331,231

// 2、将数字转为货币样式
var number = 123456.789;
console.log(number.toLocaleString(‘zh’, { style: ‘currency’, currency: ‘EUR’ })); //€123,456.79
console.log(number.toLocaleString(‘zh’, { style: ‘currency’, currency: ‘CNY’ })); //¥123,456.79
console.log(number.toLocaleString(‘zh’, { style: ‘currency’, currency: ‘CNY’,currencyDisplay:‘code’ })); //CNY 123,456.79
console.log(number.toLocaleString(‘zh’, { style: ‘currency’, currency: ‘CNY’,currencyDisplay:‘name’ })); //123,456.79人民币

// 3、将数字转为百分比形式
var num1 = 0.12
var num2 = 2.45
console.log(num1.toLocaleString(‘zh’,{style:‘percent’})) // 12%
console.log(num2.toLocaleString(‘zh’,{style:‘percent’})) // 245%

// 4、将纯数字/字符串数组所有元素用逗号拼接起来
var numArray = [12,564,‘55’,5,‘8’]
console.log(numArray.toLocaleString(‘zh’)) // 12,564,55,5,8hub.io/)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值