zoukankan      html  css  js  c++  java
  • js格式化数字

    1.保留两位小数,千分位,加上金额前缀,可以传入字符串或数字

    new Intl.NumberFormat('zh-CN', {style: 'currency', currency: 'CNY' , maximumFractionDigits: 2 }).format(123456.78967) // ¥123,456.79
    

    2.仅加千分位,可以传入字符串或数字

    new Intl.NumberFormat().format('3500.444')  // 3,500.444
    new Intl.NumberFormat().format(3500.444)  // 3,500.444
    

    3.加币种前缀

    const number = 123456.789;
    // 美元 "$123,456.79"
    new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(number) 
    // 人民币 "¥123,456.79"
    new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY' }).format(number)
    
    

    4.转化成百分数(注意,大于等于10的数字都不能被转化)

    [0.01, 1.2, 0.0123,0.1,1,1.1].map(num => {
        return new Intl.NumberFormat(undefined, { style: 'percent', maximumFractionDigits: 2 }).format(num) 
    })
    
  • 相关阅读:
    webapi之fiddler头设置
    ios---setContentOffset
    webapi参数处理get过个参数
    socket网络编程
    logging模块
    configparser模块(拷贝)
    hashlib模块--摘要算法
    异常处理
    面向对象拓展
    反射
  • 原文地址:https://www.cnblogs.com/luguankun/p/14410278.html
Copyright © 2011-2022 走看看