| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /**
- * 保留2位小数
- * @param value
- */
- var numberFormat = function (value) {
- var v = parseFloat(value)
- //强转Float,毕竟有可能返回是String类型的数字
- return v.toFixed(2)
- }
- /**
- * 保留4位小数
- * @param value
- */
- var numberFormatFour = function (value) {
- var v = parseFloat(value)
- //强转Float,毕竟有可能返回是String类型的数字
- return v.toFixed(4)
- }
- /**
- * 保留decimal位小数
- * @param value 值
- * @param decimal 位数
- */
- function toFixed(value, decimal) {
- decimal = decimal || 0;
- if (!value) return '0.00';
- return parseFloat(value).toFixed(decimal);
- }
- //取整
- function toIntFixed(value) {
- console.log(value)
- if (value == undefined) {
- value = 0
- }
- return parseInt(value);
- }
- //千分符
- function toThousandCents(num) {
- if(num == undefined){
- return 0;
- }
- var num = num + '';
- var d = '';
- if (num.slice(0, 1) == '-'){
- d = num.slice(0, 1);
- num = num.slice(1);
- }
- var len = num.length;
- var index = num.indexOf('.');
- if (index == -1) {
- num = num + '.00';
- } else if ((index + 2) == len) {
- num = num + '0';
- }
- var index = num.indexOf('.'); // 字符出现的位置
- var num2 = num.slice(-3);
- num = num.slice(0,index)
- var result = '';
- while (num.length > 3) {
- result = ',' + num.slice(-3) + result;
- num = num.slice(0, num.length - 3);
- }
- if (num) {
- result = num + result;
- }
- return d + (result + num2)
- }
- function toPercent(point){
- var percent = parseFloat(point*100).toFixed(2);
- return percent;
- }
- function toThousandCents2(num) {
- if(num == undefined){
- num = 0
- }
- num.toLocaleString()
- }
- /**
- * //暴露接口调用
- */
- module.exports = {
- toIntFixed: toIntFixed,
- toFixed: toFixed,
- numberFormat: numberFormat,
- numberFormatFour: numberFormatFour,
- toThousandCents: toThousandCents,
- toPercent: toPercent
- }
|