From 83abb309ca57c2f80145bcdeb014ed9771727e1a Mon Sep 17 00:00:00 2001 From: ForeverSmiYng Date: Thu, 27 Jun 2024 13:55:37 +0800 Subject: [PATCH] =?UTF-8?q?'=E6=B7=BB=E5=8A=A0=E9=98=BF=E6=8B=89=E4=BC=AF?= =?UTF-8?q?=E6=95=B0=E5=AD=97=E8=BD=AC=E4=B8=AD=E6=96=87=E6=95=B0=E5=AD=97?= =?UTF-8?q?'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 阿拉伯数字转中文数字.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 阿拉伯数字转中文数字.js diff --git a/阿拉伯数字转中文数字.js b/阿拉伯数字转中文数字.js new file mode 100644 index 0000000..420e440 --- /dev/null +++ b/阿拉伯数字转中文数字.js @@ -0,0 +1,25 @@ +function ArabicToChinese(Arabic_numerals) { + let strI = String(Arabic_numerals); + let mixNumerals = ""; + let positionArr = ["", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万", "十", "百", "千", "亿"]; + for (let j = 1; j <= strI.length; j++) { + let poistion = (strI.slice(-j)[0] == 0 && positionArr[j - 1] !== "万" && positionArr[j - 1] !== "亿") ? "" : positionArr[j - 1]; + let strIJ = (strI.slice(-j)[0] == 0 && (positionArr[j - 1] == "万" || positionArr[j - 1] == "亿")) ? "" : strI.slice(-j)[0]; + mixNumerals = strIJ + poistion + mixNumerals; + } + for (let j = Math.floor((strI.length - 1) / 4); j >= 1; j--) { + if (j % 2 !== 0) { + let regExp1 = new RegExp(`000+${positionArr[j * 4]}`, "g"); + mixNumerals = mixNumerals.replaceAll(regExp1, ""); + } + let regExp2 = new RegExp(`0+${positionArr[j * 4]}`, "g"); + mixNumerals = mixNumerals.replaceAll(regExp2, `${positionArr[j * 4]}0`); + } + mixNumerals = mixNumerals.replaceAll(/00+/g, "0"); + mixNumerals = mixNumerals.replace(/^1十/, "十"); + mixNumerals = mixNumerals.replace(/0+$/, ""); + mixNumerals = mixNumerals.replace(/[0-9]/g, match => { + const chineseNumerals = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]; + return chineseNumerals[parseInt(match)]; + }); +} \ No newline at end of file