vtils
Version:
一个面向业务的 JavaScript/TypeScript 实用程序库。
24 lines (23 loc) • 743 B
JavaScript
exports.__esModule = true;
exports.roundTo = roundTo;
var _lodashUni = require("lodash-uni");
/**
* 保留 n 位小数下的 x 舍 y 入。
*
* @param number 数值
* @param precision 精度
* @param threshold 舍入阈值,等于大于这个值时入,小于这个值时舍
*/
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 (0, _lodashUni.round)(+(_int + "." + decimal.slice(0, precision) + (+decimal[precision] >= threshold ? '9' : '0')), precision);
}
;