2022年的第一篇文章分享。
今天给大家介绍一下toLocalString()这个api的使用。利用toLocalString()可以为我们的开发带来很大的方便。
语法
numObj.toLocaleString([locales [options]])
locales: 指定本地环境中存在的语言类型 (字符串格式)
options: 用于指定显示的格式(对象格式)
1.数字分隔
let num1 = 1212313.12334;
num1.toLocaleString() // 1,212,313.123
2.数字转百分比并四舍五入取整
let num2 = 0.34823;
num2.toLocaleString("zh", { style: "percent" }) // 35%
1.数字分隔
let num1 = 1212313.12334;
num1.toLocaleString() // 1,212,313.123
3.数字转为货币表示法
let num3 = 1000000;
num3.toLocaleString("zh", { style: "currency", currency: "cny" })
//¥1,000,000.00
4.转化时间格式
let date = new Date();
date.toLocaleString()// 2022/1/2 下午11:05:56
// 把日期对象转化为时间戳
date.valueOf() // 1641135947939
5.将数组转化为字符串,并以逗号分隔(不适合里面有对象)
let arr = [1, 2, "3", [5, [6, [7]]]];
arr.toLocaleString()//1,2,3,5,6,7