首页 > Web开发 > 详细

js给数字加三位一逗号间隔的两种方法(面试题)

时间:2014-03-05 18:48:10      阅读:847      评论:0      收藏:0      [点我收藏+]

方法一:

 

<script  type= "text/javascript">
   //保留三位小数,toLocaleString() 方法可把一个 Number 对象转换为本地格式的字符串。
    var   num_s = "1232134456.546 ";
    alert(parseFloat(num_s).toLocaleString());

</script>

方法二:

bubuko.com,布布扣
<script type="text/javascript">
// 小数点位不限制
function format_number(nStr ){ nStr += ‘‘; x = nStr.split(.); x1 = x[0]; x2 = x.length > 1 ? . + x[1] : ‘‘; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, $1 + , + $2); } return x1 + x2; } var a="53669988.000"; alert(format_number(a)); alert(format_number("wahh")); alert(format_number(0)); alert(format_number(6698.0023)); </script>
bubuko.com,布布扣

js给数字加三位一逗号间隔的两种方法(面试题),布布扣,bubuko.com

js给数字加三位一逗号间隔的两种方法(面试题)

原文:http://www.cnblogs.com/cssfirefly/p/3582027.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!