toLocalString() 的用法

 const num = 1000000;
//整数部分每三位追加一个逗号
num.toLocaleString(); //1,000,000
//转换为百分比
num.toLocaleString("zh", { style: "percent" }); // 100000000%
//转换为货币显示
num.toLocaleString("zh", { style: "currency", currency: "CNY" }); //¥1,000,000.00
num.toLocaleString("zh", {
    style: "currency",
    currency: "cny",
    currencyDisplay: "code",
}); //CNY 1,000,000.00
num.toLocaleString("zh", {
    style: "currency",
    currency: "cny",
    currencyDisplay: "name",
}); //1,000,000.00人民币
const count = 45.6;
 //控制整数部分的最少位数和小数部分的最少和最多位数,如果不想有分隔符,可以指定useGroupingfalse
count.toLocaleString("zh", { minimumIntegerDigits: 5 }); //00,045.6
count.toLocaleString("zh", { minimumIntegerDigits: 5, useGrouping: false }); //00045.6
count.toLocaleString("zh", { minimumFractionDigits: 6 }); //45.600000
count.toLocaleString("zh", { maximumFractionDigits: 0 }); //46
//控制有效数字最少和最多位数
count.toLocaleString("zh", { minimumSignificantDigits: 5 }); //45.600
count.toLocaleString("zh", { maximumSignificantDigits: 2 }); //46
const date = new Date();
//格式化日期,12小时制
date.toLocaleString("zh", { hour12: true }); //2022/2/7 下午6:03:05
//格式化日期,24小时制
date.toLocaleString("zh", { hour12: false }); //2022/2/7 18:04:12
date.toLocaleString("zh", { timeZoneName: "short" }); //2022/2/7 GMT+8 下午6:07:32
date.toLocaleString("zh", { timeZoneName: "long" }); //2022/2/7 中国标准时间 下午6:08:14
//是否仅用两位数字表示
    year: "numeric",
    month: "numeric",
    day: "numeric",
    hour: "numeric",
    minute: "numeric",
    second: "numeric",
}); //2022/2/7 下午6:11:13
    date.toLocaleString("zh", {
        year: "2-digit",
        month: "2-digit",
        day: "2-digit",
        hour: "2-digit",
        minute: "2-digit",
        second: "2-digit",
    }); //22/02/07 下午06:12:05
//设置语言对月份的不同展现
date.toLocaleString('en',{ month:'narrow' }) // F
date.toLocaleString('en',{ month:'short' }) //Feb
date.toLocaleString('en',{ month:'long' }) //February

 const num = 1000000;
//整数部分每三位追加一个逗号
num.toLocaleString(); //1,000,000
//转换为百分比
num.toLocaleString("zh", { style: "percent" }); // 100000000%
//转换为货币显示
num.toLocaleString("zh", { style: "currency", currency: "CNY" }); //¥1,000,000.00
num.toLocaleString("zh", {
    style: "currency",
    currency: "cny",
    currencyDisplay: "code",
}); //CNY 1,000,000.00
num.toLocaleString("zh", {
    style: "currency",
    currency: "cny",
    currencyDisplay: "name",
}); //1,000,000.00人民币
const count = 45.6;
 //控制整数部分的最少位数和小数部分的最少和最多位数,如果不想有分隔符,可以指定useGroupingfalse
count.toLocaleString("zh", { minimumIntegerDigits: 5 }); //00,045.6
count.toLocaleString("zh", { minimumIntegerDigits: 5, useGrouping: false }); //00045.6
count.toLocaleString("zh", { minimumFractionDigits: 6 }); //45.600000
count.toLocaleString("zh", { maximumFractionDigits: 0 }); //46
//控制有效数字最少和最多位数
count.toLocaleString("zh", { minimumSignificantDigits: 5 }); //45.600
count.toLocaleString("zh", { maximumSignificantDigits: 2 }); //46
const date = new Date();
//格式化日期,12小时制
date.toLocaleString("zh", { hour12: true }); //2022/2/7 下午6:03:05
//格式化日期,24小时制
date.toLocaleString("zh", { hour12: false }); //2022/2/7 18:04:12
date.toLocaleString("zh", { timeZoneName: "short" }); //2022/2/7 GMT+8 下午6:07:32
date.toLocaleString("zh", { timeZoneName: "long" }); //2022/2/7 中国标准时间 下午6:08:14
//是否仅用两位数字表示
    year: "numeric",
    month: "numeric",
    day: "numeric",
    hour: "numeric",
    minute: "numeric",
    second: "numeric",
}); //2022/2/7 下午6:11:13
    date.toLocaleString("zh", {
        year: "2-digit",
        month: "2-digit",
        day: "2-digit",
        hour: "2-digit",
        minute: "2-digit",
        second: "2-digit",
    }); //22/02/07 下午06:12:05
//设置语言对月份的不同展现
date.toLocaleString('en',{ month:'narrow' }) // F
date.toLocaleString('en',{ month:'short' }) //Feb
date.toLocaleString('en',{ month:'long' }) //February

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值