js 数字,金额 用逗号 隔开数字格式化.docx

上传人:小飞机 文档编号:3159966 上传时间:2023-03-11 格式:DOCX 页数:3 大小:36.86KB
返回 下载 相关 举报
js 数字,金额 用逗号 隔开数字格式化.docx_第1页
第1页 / 共3页
js 数字,金额 用逗号 隔开数字格式化.docx_第2页
第2页 / 共3页
js 数字,金额 用逗号 隔开数字格式化.docx_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《js 数字,金额 用逗号 隔开数字格式化.docx》由会员分享,可在线阅读,更多相关《js 数字,金额 用逗号 隔开数字格式化.docx(3页珍藏版)》请在三一办公上搜索。

1、js 数字,金额 用逗号 隔开。数字格式化js 数字,金额 用逗号 隔开。数字格式化 例如: 12345格式化为12,345.00 12345.6格式化为12,345.60 12345.67格式化为 12,345.67 只留两位小数。 回来后写了个格式化函数。可以控制小数位数,自动四舍五入。 代码如下: 引用 function fmoney(s, n) n = n 0 & n = 20 ? n : 2; s = parseFloat(s + ).replace(/d.-/g, ).toFixed(n) + ; var l = s.split(.)0.split().reverse, r = s

2、.split(.)1; t = ; for(i = 0; i l.length; i + ) t += li + (i + 1) % 3 = 0 & (i + 1) != l.length ? , : ); return t.split().reverse.join() + . + r; 调用:fmoney(12345.675910, 3),返回12,345.676 还原函数: 引用 function rmoney(s) return parseFloat(s.replace(/d.-/g, ); 示例: 引用 function fmoney(s, n) n = n 0 & n = 20 ?

3、n : 2; s = parseFloat(s + ).replace(/d.-/g, ).toFixed(n) + ; var l = s.split(.)0.split().reverse, r = s.split(.)1; t = ; for(i = 0; i l.length; i + ) t += li + (i + 1) % 3 = 0 & (i + 1) != l.length ? , : ); return t.split().reverse.join() + . + r; function rmoney(s) return parseFloat(s.replace(/d.-/

4、g, ); function g(id) return document.getElementById(id); window.onload = function var num, txt = g(txt), txt2 = g(txt2), btn = g(btn), btn2 = g(btn2), span = g(span); btn.onclick = function num = parseInt(g(num).value); txt.value = fmoney(txt.value, num); txt2.value = fmoney(txt2.value, num); ; btn2.onclick = function num = parseInt(g(num).value); span.innerHTML = = + fmoney(rmoney(txt.value) + rmoney(txt2.value), num); ; ; 小数点位数: 2 3 4 5 +

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号