fenzhi-utils
Version:
分值前端项目的js函数库
24 lines (23 loc) • 776 B
JavaScript
/**
* 保留小数(非四舍五入)
* @param {*} n
* @param {*} fixed
* @returns
*/
/**
console.log(CustomNumberNoFixed(10.123456, 2)); // 10.12
console.log(CustomNumberNoFixed(-10.123456, 2)); // -10.12
console.log(CustomNumberNoFixed(NaN, 2)); // -
console.log(CustomNumberNoFixed(null, 2)); // -
console.log(CustomNumberNoFixed('', 2)); // -
console.log(CustomNumberNoFixed(undefined, 2)); // -
console.log(CustomNumberNoFixed(Number.MAX_SAFE_INTEGER, 2)); // 9007199254740991
console.log(CustomNumberNoFixed(Number.MIN_SAFE_INTEGER, 2)); // -9007199254740991
*/
export function CustomNumberNoFixed(n, fixed) {
const num = parseFloat(n);
if (isNaN(num)) {
return '-';
}
return ~~(Math.pow(10, fixed) * num) / Math.pow(10, fixed);
}