UNPKG

@arco-vue-pro-components/pro-components

Version:
113 lines (112 loc) 3.11 kB
function formatterDecimal(value, precision = 2, capital = true, intPartNumber = 15) { if (typeof value !== "string" || !value) { return { value: "", text: "" }; } const arr = value.split(".", 2); let newValue; let newValueText; let intPart = arr[0]; if (arr[0].length > intPartNumber) { intPart = arr[0].substr(0, intPartNumber); } newValue = intPart; if (capital) { newValueText = `${intPart}`.replace(/\B(?=(\d{3})+(?!\d))/g, ","); } else { newValueText = intPart; } if (arr[1]) { const num = arr[1].substr(0, precision); newValue = `${newValue}${precision === 0 ? "" : "."}${num}`; newValueText = `${newValueText}${precision === 0 ? "" : "."}${num}`; } else if (precision > 0 && value.indexOf(".") > -1) { newValue += "."; newValueText += "."; } return { value: newValue, text: newValueText }; } function toCapital(money, unit, precision = 2) { const cnNums = ["\u96F6", "\u58F9", "\u8D30", "\u53C1", "\u8086", "\u4F0D", "\u9646", "\u67D2", "\u634C", "\u7396"]; const cnIntRadice = ["", "\u62FE", "\u4F70", "\u4EDF"]; const cnIntUnits = ["", "\u4E07", "\u4EBF", "\u5146"]; const cnDecUnits = ["\u89D2", "\u5206", "\u6BEB", "\u5398"]; const cnInteger = "\u6574"; const cnIntLast = "\u5143"; const maxNum = 1e15; let IntegerNum; let DecimalNum; let ChineseStr = ""; let Symbol = ""; if (money === "") { return ""; } if (!money) { return ""; } money = parseFloat(money); switch (unit) { case "\u4E07\u5143": money = money * 1e6 / 100; break; } if (money >= maxNum) { return ""; } if (money === 0) { ChineseStr = cnNums[0] + cnIntLast + cnInteger; return ""; } if (money < 0) { money = -money; Symbol = "\u8D1F "; } money = money.toString(); if (money.indexOf(".") === -1) { IntegerNum = money; DecimalNum = ""; } else { const [part1, part2] = money.split("."); IntegerNum = part1; DecimalNum = part2.substr(0, precision); } if (parseInt(IntegerNum, 10) > 0) { let zeroCount = 0; const IntLen = IntegerNum.length; for (let i = 0; i < IntLen; i++) { const n = IntegerNum.substr(i, 1); const p = IntLen - i - 1; const q = p / 4; const m = p % 4; if (n === "0") { zeroCount++; } else { if (zeroCount > 0) { ChineseStr += cnNums[0]; } zeroCount = 0; ChineseStr += cnNums[parseInt(n, 10)] + cnIntRadice[m]; } if (m === 0 && zeroCount < 4) { ChineseStr += cnIntUnits[q]; } } ChineseStr += cnIntLast; } if (DecimalNum !== "") { const decLen = DecimalNum.length; for (let j = 0; j < decLen; j++) { const nn = DecimalNum.substr(j, 1); if (nn !== "0") { ChineseStr += cnNums[Number(nn)] + cnDecUnits[j]; } } } if (ChineseStr === "") { ChineseStr += cnNums[0] + cnIntLast + cnInteger; } else if (DecimalNum === "") { ChineseStr += cnInteger; } ChineseStr = Symbol + ChineseStr; return ChineseStr; } export { formatterDecimal, toCapital };