分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

JS数字分割

发布时间:2023-09-06 01:42责任编辑:顾先生关键词:暂无标签
 1 //将所有的数字金额每3位添加一个逗号,示例:888,888,888.8 2 ?3 //以下是整理的干货: 4 ?5 function formatNum(str){ 6 ?7 var newStr = ""; 8 ?9 var count = 0;10 11 12 13 if(str.indexOf(".")==-1){14 15 for(var i=str.length-1;i>=0;i--){16 17 if(count % 3 == 0 && count != 0){18 19 newStr = str.charAt(i) + "," + newStr;20 21 }else{22 23 newStr = str.charAt(i) + newStr;24 25 }26 27 count++;28 29 }30 31 str = newStr + ".00"; //自动补小数点后两位32 33 return str;34 35 }36 37 else38 39 {40 41 for(var i = str.indexOf(".")-1;i>=0;i--){42 43 if(count % 3 == 0 && count != 0){44 45 newStr = str.charAt(i) + "," + newStr;46 47 }else{48 49 newStr = str.charAt(i) + newStr; //逐个字符相接起来50 51 }52 53 count++;54 55 }56 57 str = newStr + (str + "00").substr((str + "00").indexOf("."),3);58 59 return str;60 61 }62 63 }

JS数字分割

原文地址:https://www.cnblogs.com/mhxy13867806343/p/8447156.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved