js以千位分隔符方式格式化数字number_format

技术探讨  2016-08-12 23:43   9679 JS 格式化

//number_format  以千位分隔符方式格式化一个数字

function number_format(number,decimals,dec_point,thousands_sep) {
  if(decimals){
    number = parseFloat(number).toFixed(decimals);   //转化为Float浮点型 并保留指定小数位
  }
  dec_point = dec_point ? dec_point : '.';
  thousands_sep = thousands_sep ? thousands_sep : ',';
  var source = String(number).split(".");
      source[0] = source[0].replace(new RegExp('(\\d)(?=(\\d{3})+$)','ig'),"$1"+thousands_sep);
  return source.join(dec_point);
}
var num = 54623546.547;
document.write(number_format(num,2,'.',','));



注:本文转载自原创,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。

沙豆网 站长

追求卓越,奋斗不息!

167
文章
8799
点赞

更多文章