toLocaleString()使用

 1  const num = 1000000;
 2   //整数部分每三位追加一个逗号
 3   num.toLocaleString(); //1,000,000
 4   //转换为百分比
 5   num.toLocaleString("zh", { style: "percent" }); // 100000000%
 6   //转换为货币显示
 7   num.toLocaleString("zh", { style: "currency", currency: "CNY" }); //¥1,000,000.00
 8   num.toLocaleString("zh", {
 9     style: "currency",
10     currency: "cny",
11     currencyDisplay: "code",
12   }); //CNY 1,000,000.00
13   num.toLocaleString("zh", {
14     style: "currency",
15     currency: "cny",
16     currencyDisplay: "name",
17   }); //1,000,000.00人民币
18 
19   const count = 45.6;
20   //控制整数部分的最少位数和小数部分的最少和最多位数,如果不想有分隔符,可以指定useGrouping为false
21   count.toLocaleString("zh", { minimumIntegerDigits: 5 }); //00,045.6
22   count.toLocaleString("zh", { minimumIntegerDigits: 5, useGrouping: false }); //00045.6
23   count.toLocaleString("zh", { minimumFractionDigits: 6 }); //45.600000
24   count.toLocaleString("zh", { maximumFractionDigits: 0 }); //46
25   //控制有效数字最少和最多位数
26   count.toLocaleString("zh", { minimumSignificantDigits: 5 }); //45.600
27   count.toLocaleString("zh", { maximumSignificantDigits: 2 }); //46
28 
29   const date = new Date();
30   //格式化日期,12小时制
31   date.toLocaleString("zh", { hour12: true }); //2022/2/7 下午6:03:05
32   //格式化日期,24小时制
33   date.toLocaleString("zh", { hour12: false }); //2022/2/7 18:04:12
34   date.toLocaleString("zh", { timeZoneName: "short" }); //2022/2/7 GMT+8 下午6:07:32
35   date.toLocaleString("zh", { timeZoneName: "long" }); //2022/2/7 中国标准时间 下午6:08:14
36   //是否仅用两位数字表示
37   date.toLocaleString("zh", {
38     year: "numeric",
39     month: "numeric",
40     day: "numeric",
41     hour: "numeric",
42     minute: "numeric",
43     second: "numeric",
44   }); //2022/2/7 下午6:11:13
45   date.toLocaleString("zh", {
46     year: "2-digit",
47     month: "2-digit",
48     day: "2-digit",
49     hour: "2-digit",
50     minute: "2-digit",
51     second: "2-digit",
52   }); //22/02/07 下午06:12:05
53   //设置语言对月份的不同展现
54   date.toLocaleString('en',{ month:'narrow' }) // F
55   date.toLocaleString('en',{ month:'short' }) //Feb
56   date.toLocaleString('en',{ month:'long' }) //February

 

上一篇:c# BackgroundWorker初试


下一篇:JDK1.5新特性(七)……Annotations