const RetainDecimal = (num, count, type) => {  // num -> 需要保留的小数, count -> 需要保留的位数, type -> 保留的类型, eg: 'floor': 向下取整,  默认是四舍五入
const ratio = 10 ** count || 100;
const rest = `${ratio}`.slice(1);
if (isNaN(Number(num))) {
return num;
} else if (num) {
const newNum = type === 'floor' ? Math.floor(num * ratio) / ratio : Math.round(num * ratio) / ratio;
if (`${newNum}`.indexOf('.') < 0) {
return `${newNum}.${rest}`;
} else if (`${newNum}`.indexOf('.') > -1) {
const digitCount = `${newNum}`.split('.')[1].length;
if (digitCount < count) {
return `${newNum}${`${ratio}`.slice(digitCount + 1)}`;
} else {
return newNum;
}
}
return newNum;
}
return false;
};

 posted on 2017-11-27 19:19  weimo10235  阅读(915)  评论(0)    收藏  举报