vtils
Version:
一个面向业务的 JavaScript/TypeScript 实用程序库。
21 lines (20 loc) • 662 B
JavaScript
import { round } from 'lodash-uni';
/**
* 保留 n 位小数下的 x 舍 y 入。
*
* @param number 数值
* @param precision 精度
* @param threshold 舍入阈值,等于大于这个值时入,小于这个值时舍
*/
export function roundTo(number, precision, threshold) {
if (precision === void 0) {
precision = 0;
}
if (threshold === void 0) {
threshold = 5;
}
var _number$toFixed$split = number.toFixed(precision + 2).split('.'),
_int = _number$toFixed$split[0],
decimal = _number$toFixed$split[1];
return round(+(_int + "." + decimal.slice(0, precision) + (+decimal[precision] >= threshold ? '9' : '0')), precision);
}