jrj-front-end-tool
Version:
jrj前端工具包
34 lines (33 loc) • 944 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.keepCustomDecimalStr = void 0;
/**
* 精确显示小数点后n位(不进行4舍5入),没有时补0
* e.g:
* value:123.16,num:1 ----> 123.1
* value:123.16,num:3 ----> 123.160
* @param value 传入的数字
* @param num 要精确显示多少位
* @returns
*/
var keepCustomDecimalStr = function (value, num) {
var s = value.toString();
var rs = s.indexOf('.');
if (rs < 0 && num > 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + num) {
s += '0';
}
if (num > 0) {
// 如果value的小数超过了num,进行截取
s = s.slice(0, s.indexOf('.') + num + 1);
}
if (num === 0 && s.indexOf('.') >= 0) {
// 舍去全部小数的情况
s = s.slice(0, s.indexOf('.') + num);
}
return s;
};
exports.keepCustomDecimalStr = keepCustomDecimalStr;