js原生的只有四舍五入,即toFixed(n),且无法保留末尾的0,想要其它的只能自己写。
四舍五入是最常见的,四舍六入五凑偶(银行家取舍法)会计中常用,还有就是截取指定位数
原生js编写,可使用在任何js体系内,无需任何外部库或者模块(建议处理后放到类中使用)
废话不多说,下面展示 代码
。注释中有说明。
/**
* 数字格式处理(可以处理包含非数字及首位为.的数字)
* @param num {string|number} 输入的数字(字符串或数字形式)
* @param place {number} 保留小数点后几位数字
* @param type {string} 截取类型,'45'-四舍五入(默认),'46'-四舍六入五凑偶,'cut'或其他-直接截取
* @param zero {boolean} 是否保留小数点后面的末尾0,true-保留(默认),false-不保留
* @return {string|boolean} 成功返回字符串形式的数字,失败返回false
* @version 1.0
* @author ziqibit 2023/03/10
**/
function numFormat(num, place, type = '45', zero = true) {
let num_str = num.toString();
let fu_noyes = num_str.slice(0, 1) === '-';
num_str = num_str.replace(/[^\d.]/g, "");
const num_dot = num_str.split(".").length - 1;
const str_len = num_str.length;
const pl = num_str.indexOf(".");
const len = str_len - pl - 1
if (num_dot > 1) {
return false
}
if (place < 0) {
return false
}
if (pl === -1) {
if (place !== 0 && zero) {
num_str += '.';
num_str += '0'.repeat(place)
}
return num_str
} else if (pl >= 0) {
if (place === 0 && pl === 0) {
return false
}
if (type === '45') {
let num2 = num_str.length;
let zero_num = str_len - num2 + place - len;
if (pl === 0) {
num_str = '0' + num_str;
zero_num++
}
num_str = (Math.round(parseFloat(num_str) * Math.pow(10, place)) / Math.pow(10, place)).toFixed(place);
if (len < place && zero && zero_num > 0) {
num_str += '0'.repeat(zero_num)
}
if (!zero) {
num_str = parseFloat(num_str).toString()
}
} else if (type === '46') {
let num2 = num_str.length;
let zero_num = str_len - num2 + place - len;
if (zero_num >= 0) {
if (pl === 0) {
num_str = '0' + num_str;
zero_num++
}
num_str = (Math.round(parseFloat(num_str) * Math.pow(10, place)) / Math.pow(10, place)).toString();
if (len < place && zero && zero_num > 0) {
num_str += '0'.repeat(zero_num)
}
} else if (zero_num === -1) {
const last_num = num_str.slice( - 1);
if (last_num === '5') {
const last2_num = num_str.slice(num_str.length - 2, num_str.length - 1);
if (parseInt(last2_num) % 2 === 1) {
num_str = num_str.slice(0, num_str.length - 1);
let num_add = parseFloat('0.0'.repeat(place - 1) + '1');
num_str = (parseFloat(num_str) + num_add).toFixed(place);
if (!zero) {
num_str = parseFloat(num_str).toString()
}
} else {
num_str = num_str.slice(0, num_str.length - 1)
}
} else {
num_str = (Math.round(parseFloat(num_str) * Math.pow(10, place)) / Math.pow(10, place)).toString()
}
} else {
const abandon1_str = num_str.slice(num2 - zero_num, num2 - zero_num + 1);
if (abandon1_str === '5') {
let num_add = parseFloat('0.0'.repeat(place - 1) + '1');
if (!zero) {
num_str = ((parseFloat(num_str) + num_add) * Math.pow(10, place)) / Math.pow(10, place).toString()
} else {
num_str = (parseFloat(num_str) + num_add).toFixed(place)
}
} else {
num_str = (Math.round(parseFloat(num_str) * Math.pow(10, place)) / Math.pow(10, place)).toString()
}
}
} else {
if (pl === 0) {
num_str = '0' + num_str;
num_str = num_str.slice(0, place + 2)
} else {
num_str = num_str.slice(0, str_len - len + place)
}
if (len < place && zero) {
num_str += '0'.repeat(place - len)
}
if (!zero) {
num_str = parseFloat(num_str).toString()
}
}
return fu_noyes ? '-' + num_str: num_str
} else {
return false
}
}
使用方法也很简单
//四舍五入,保留2位,末尾0保留
console.log(numFormat('12345.6955',2));//12345.70
//四舍六入五凑偶,保留2位小数,不保留末尾0
console.log(numFormat('123.456789',2,'46',false));//123.46
//截取法,截取4位小数,不保留末尾0
console.log(numFormat('123.456000',4,'cut',false));//123.456