/**
* [number_format 参数说明:]
* @param {[type]} number [number:要格式化的数字]
* @param {[type]} decimals [decimals:保留几位小数]
* @param {[type]} dec_point [dec_point:小数点符号]
* @param {[type]} thousands_sep [thousands_sep:千分位符号]
* @param {[type]} roundtag [roundtag:舍入参数,默认 "ceil" 向上取,"floor"向下取,"round" 四舍五入]
* @return {[type]} [description]
*
*/
function number_format(number, decimals, dec_point, thousands_sep, roundtag) { number = (number + '').replace(/[^0-9+-Ee.]/g, '');
roundtag = roundtag || "ceil"; //"ceil","floor","round"
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function(n, prec) { var k = Math.pow(10, prec);
console.log(); return '' + parseFloat(Math[roundtag](parseFloat((n * k).toFixed(prec * 2))).toFixed(prec * 2)) / k;
};
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
var re = /(-?\d+)(\d{3})/;
while (re.test(s[0])) {
s[0] = s[0].replace(re, "$1" + sep + "$2");
} if ((s[1] || '').length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
return s.join(dec);
}
console.log(number_format(2, 2, ".", ",")) //"2.00"
console.log(number_format(3.7, 2, ".", ",")) //"3.70"
console.log(number_format(3, 0, ".", ",")) //"3"
console.log(number_format(9.0312, 2, ".", ",")) //"9.03"
console.log(number_format(9.00, 2, ".", ",")) //"9.00"
console.log(number_format(39.715001, 2, ".", ",", "floor")) //"39.71"
console.log(number_format(9.7, 2, ".", ",")) //"9.70"
console.log(number_format(39.7, 2, ".", ",")) //"39.70"
console.log(number_format(9.70001, 2, ".", ",")) //"9.71"
console.log(number_format(39.70001, 2, ".", ",")) //"39.71"
console.log(number_format(9996.03, 2, ".", ",")) //"9996.03"
console.log(number_format(1.797, 3, ".", ",", "floor")) //"1.797"

参数有点多,你可以根据你自己的需求去修改。

推荐的类库 Numeral.js 和 accounting.js

Numeral.js

一个用于格式化和操作数字的JavaScript库。数字可以被格式化为货币,百分比,时间,几个小数位数,千分位等等。 您也可以随时创建自定义格式。

官网及文档:http://numeraljs.com/

GitHub:https://github.com/adamwdraper/Numeral-js

accounting.js

一个轻量级的JavaScript库,用于格式化数字,金额和货币等。

官网及文档:http://openexchangerates.github.io/accounting.js/

GitHub:accounting.js

如果这篇文章对您有帮助,您可以打赏我

JS实现数字千位符格式化方法的更多相关文章

  1. wpf 千位符 格式化字符串

    StringFormat={}{0:N2}}//格式话字符串,增加千位符,2保留小数点后2位 StringFormat={}{0:N0}}//格式话字符串,增加千位符,无小数点后

  2. js实现千位符分隔

    前几天面试做保险项目的公司,被问到了一道实现千位符分割方法的题,乍一看挺简单,但做起来最后却没给出来一个合适的解决方法.回来自己琢磨了一个还行的答案. var num = 3899000001, ar ...

  3. js千位符 | js 千位分隔符 | js 金额格式化

    js 千位分隔符 千位分隔符,其实就是数字中的逗号.依西方的习惯,人们在数字中加进一个符号,以免因数字位数太多而难以看出它的值.所以人们在数字中,每隔三位数加进一个逗号,也就是千位分隔符,以便更加容易 ...

  4. 对Textbox的值转换为带千位符和小数的Decimal字符串

    以下Function可以用于textbox的KeyUp事件: 2014-06-06 发现旧版IE不支持selectionStart还有字符串的"[]"索引获取值, 已经修复这个bu ...

  5. JS 正则中环视(断言)应用 -- 数字千分符

    介绍一下顺序环视 (?=...) 和逆序环视 (?<=...) 方便不想看长文的人,如果在支持 ES2018 的环境中整数可以这样使用: String(12345678).replace(/(? ...

  6. 使用js在HTML中自定义字符串格式化方法

    Python中支持字符串格式化,其基本形式如下: str = "I'm {name},{age} years old" print(str.format(name="te ...

  7. JS自动格式化输入的数字/千位分隔符

    <script> function cc(s){         if(/[^0-9\.]/.test(s)) return "invalid value";     ...

  8. JS自动格式化输入的数字/千位分隔符VIEW:858

    <script> function cc(s){ if(/[^0-9\.]/.test(s)) return "invalid value"; ss=s.replace ...

  9. MySQL中大数字加减,不产生千位符和科学计数

    mysql数字加减科学计数法 这两天因为需求,需要获取一张表的流水号.规则是这样的.当前日期+8位流水号.比如:2015062400000001,2015062400000002,2015062400 ...

随机推荐

  1. linux -- ubuntu桌面版安装xampp

    首先,请从www.xampp.org下载最新版XAMPP. 安装 如果是xampp压缩文件 将xampp压缩文件复制到/opt下并解压.如果你计算机没有/opt目录,用 “sudo mkdir/opt ...

  2. Storm On YARN带来的优点

    1)弹性计算资源     将storm执行在yarn上后,Storm能够与其它计算框架(如mapreduce)共享整个集群的资源.这样当Storm负载骤增时,可动态为它添加计算资源. 负载减小时,能够 ...

  3. c++ list reverse_iterator

    #pragma warning(disable:4786) #include <set> #include <iostream> using namespace std ; t ...

  4. BIEE多层表头报表的制作方法

    使用BIEE制作多层表头的报表大致分为以下的几种办法. 1.双层表头 这种比较容易实现,只需要在表格属性中勾选作为单独的行显示即可.这样通过修改文件夹标题,就可以实现双层表头的制作 2.多层表头的制作 ...

  5. ali数据分析面试题

    表A结构如下 Member_ID   --用户的Id,字符型 Log_time  --用户访问页面时间,日期型(只有一天的数据) URL ---访问的页面地址,字符型 要求:提取每个用户访问的第一个U ...

  6. Linux Shell脚本面试25个经典问答

    1 Shell脚本是什么.它是必需的吗? 答:一个Shell脚本是一个文本文件,包含一个或多个命令.作为系统管理员,我们经常需要使用多个命令来完成一项任务,我们可以添加这些所有命令在一个文本文件(Sh ...

  7. JQuery--使用autocomplete控件进行自己主动输入完毕(相当于模糊查询)

    之前为了实现这个功能花了我几天的时间. 事实上.实现了之后发现也就那么回事,正所谓万事开头难嘛.. 废话不多说了.这里我使用的是JQuery控件库中的一个Autocomplete控件.即Autocom ...

  8. vue实现图片点击放大

    用的vue-cli开发的项目,下面是具体实现代码 子组件: <template> <!-- 过渡动画 --> <transition name="fade&qu ...

  9. Windows7 64bits下安装TensorFlow CPU版本(图文详解)

    不多说,直接上干货! Installing TensorFlow on Windows的官网 https://www.tensorflow.org/install/install_windows 首先 ...

  10. python2.0_day19_后台数据库设计思路

    from django.db import models # Create your models here. from django.contrib.auth.models import User ...