zoukankan      html  css  js  c++  java
  • 数字每三位加逗号

    function formatNum(str) { var newStr = ""; var count = 0; // 当数字是整数 if (str.indexOf(".") == -1) { for (var i = str.length - 1; i >= 0; i--) { if (count % 3 == 0 && count != 0) { newStr = str.charAt(i) + "," + newStr; } else { newStr = str.charAt(i) + newStr; } count++; } str = newStr + ".00"; //自动补小数点后两位 return str; } // 当数字带有小数 else { for (var i = str.indexOf(".") - 1; i >= 0; i--) { if (count % 3 == 0 && count != 0) { newStr = str.charAt(i) + "," + newStr; } else { newStr = str.charAt(i) + newStr; //逐个字符相接起来 } count++; } str = newStr + (str + "00").substr((str + "00").indexOf("."), 3); return str; } } formatNum('13213.24'); //输出13,213.34 formatNum('132134.2'); //输出132,134.20 formatNum('132134'); //输出132,134.00 formatNum('132134.236'); //输出132,134.23




  • 相关阅读:
    app 后端技术
    别为大公司拼命
    ifconfig 工具
    route工具
    ping 和 traceroute 命令
    IP路由选择
    TCP的那些事儿(下)
    TCP的那些事儿(上)
    Openresty 与 Tengine
    Excel.Application SaveAs 把excel转换为html
  • 原文地址:https://www.cnblogs.com/zouyun/p/9679574.html
Copyright © 2011-2022 走看看