1. O número retém duas casas decimais, reescreva o método toFixed
/**
* @description:保留两位小数,可直接截取,可四舍五入 重置Number 的 toFixed()方法
* @param {Number} decimals 小数位数,默认2,最多10
* @return {Boolean} isAddZero 不够小数位是否添0 默认不添加
* @return {Boolean} isRounding 是否四舍五入,默认是
* @example 123.10293.toFixed(2,true) => 123.10
* @Author: liuxin
*/
Number.prototype.toFixed = function (decimals = 2, isAddZero = false, isRounding = true) {
const preNum = this;
if (isNaN(preNum) || (!preNum && preNum !== 0)) {
return ''
}
// 最多10位小数
if (decimals > 10) {
decimals = 10;
}
// 先进行四舍五入
let num = 0;
if (isRounding === true) {
const decimalsNumTemp = Math.pow(10, decimals); // 取保留的小数最大整数值 如10 100 1000等
num = Math.round(parseFloat(preNum) * decimalsNumTemp) / decimalsNumTemp;
}
// 默认为保留的小数点后两位
let tempNum = Number(num)
let pointIndex = String(tempNum).indexOf('.') + 1 // 获取小数点的位置 + 1
let pointCount = pointIndex ? String(tempNum).length - pointIndex + 1 : 0 // 获取小数点后的个数(需要保证有小数位)
// 小数位小于需要截取的位数
if (pointIndex === 0 || pointCount <= decimals) {
// 源数据为整数或者小数点后面小于decimals位的作补零处理
if (isAddZero === true) {
let tempNumA = tempNum
if (pointIndex === 0) {
tempNumA = `${tempNumA}.`
for (let index = 0; index < decimals - pointCount; index++) {
tempNumA = `${tempNumA}0`
}
} else {
for (let index = 0; index < decimals - pointCount; index++) {
tempNumA = `${tempNumA}0`
}
}
return tempNumA;
}
return tempNum
}
let realVal = '';
// 截取当前数据到小数点后decimals位
realVal = `${String(tempNum).split('.')[0]}.${String(tempNum)
.split('.')[1]
.substring(0, decimals)}`
// 判断截取之后数据的数值是否为0
if (realVal == 0) {
realVal = 0
}
return realVal
}
2. Filtro de método de filtro
/**
* @description 过滤选项值
* @param {Object} obj 需要判断的对象
* @param {*} value 值
* @param {Array[Object]} options 需过滤的选项
* @param {String} emptyString 空字符占位
* @param {Object} prop 对应过滤的属性 默认 {text:'text',value:'value'}
* {text:'',value:''} => return text
* @author liuxin
*/
export function filterOptions(value, options, emptyString = "", prop = {
text: 'text',
value: 'value'
}) {
if (value === undefined || value === null) {
return emptyString;
}
let returnValue = emptyString;
for (const i in options) {
if (options[i][prop.value] === value) {
returnValue = options[i][prop.text];
break;
}
}
return returnValue;
}
3. Obter data
3.1 Obtenha o ano, mês e dia atuais
/**
* @description: 获取当前年月日
* @param {String} splitString 分割符号
* @return {String} 2021-8-18
* @Author: liuxin
*/
export function getCurrentDate(splitString = "-") {
const date = new Date();
const month = date.getMonth() + 1;
const day = date.getDate();
return date.getFullYear() + splitString +
(month < 10 ? ('0' + month) : month) + splitString +
(day < 10 ? ('0' + day) : day)
}
3.2 Obter o número de dias em dois períodos de tempo
/**
* @description: 获取两个时间内的天数
* @param {String} startTime 开始日期 2021-01-01
* @param {String} endTime 结束日期 2021-01-01
* @return {Number} 2021-01-01,2021-01-01 结果是1
* @Author: liuxin
*/
export function getDayByTwoDate(startTime, endTime) {
if (!startTime || !endTime) {
return null;
}
const startDate = new Date(startTime).getTime();
const endDate = new Date(endTime).getTime();
return (endDate - startDate) / 1000 / 60 / 60 / 24 + 1; // 自动加一天,从当日算起
}
4. Baixar arquivos
/**
* @description 下载文件
* @param {file} fileByte 字节流
* @param {String} fileName 文件名称
* @example downFile(**,"测试.doc")
* @author liuxin
*/
export function downFile(fileByte, fileName) {
if (!fileByte) {
return;
}
let url = window.URL.createObjectURL(new Blob([fileByte]));
let link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
}
5. Converta o valor para letras maiúsculas
/** 金额转大写 */
export const digitUppercase = money => {
if (money === "" || money === undefined || money === null) {
return "";
}
const fraction = ["角", "分"];
const digit = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
const unit = [
["元", "万", "亿"],
["", "拾", "佰", "仟"]
];
const head = money < 0 ? "负" : "";
money = Math.abs(money);
let s = "";
for (let i = 0; i < fraction.length; i++) {
s += (
digit[Math.floor(money * 10 * Math.pow(10, i)) % 10] + fraction[i]
).replace(/零./, "");
}
s = s || "整";
money = Math.floor(money);
for (let i = 0; i < unit[0].length && money > 0; i++) {
let p = "";
for (let j = 0; j < unit[1].length && money > 0; j++) {
p = digit[money % 10] + unit[1][j] + p;
money = Math.floor(money / 10);
}
s = p.replace(/(零.)*零$/, "").replace(/^$/, "零") + unit[0][i] + s;
}
return (
head +
s
.replace(/(零.)*零元/, "元")
.replace(/(零.)+/g, "零")
.replace(/^整$/, "零元整")
);
};