/** * 保留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) { 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 }