<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
//************************* 把數字金額轉換成中文大寫數字的函數(可處理負值) *********************************//
function changeNumMoneyToChinese(money)
{
var cnNums = new Array("零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"); //漢字的數字
var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本單位
var cnIntUnits = new Array("", "萬", "億", "兆"); //對應整數部分擴展單位
var cnDecUnits = new Array("角", "分", "毫", "厘"); //對應小數部分單位
var cnInteger = "整"; //整數金額時后面跟的字符
var cnIntLast = "元"; //整型完以后的單位
var maxNum = 999999999999999.9999; //最大處理的數字
var IntegerNum; //金額整數部分
var DecimalNum; //金額小數部分
var ChineseStr = ""; //輸出的中文金額字符串
var parts; //分離金額后用的數組,預定義
var Symbol="";//正負值標記
if (money == "") {
return "";
}
money = parseFloat(money);
if (money >= maxNum) {
alert('超出最大處理數字');
return "";
}
if (money == 0) {
ChineseStr = cnNums[0] + cnIntLast + cnInteger;
return ChineseStr;
}
if(money<0)
{
money=-money;
Symbol="負 ";
}
money = money.toString(); //轉換為字符串
if (money.indexOf(".") == -1) {
IntegerNum = money;
DecimalNum = '';
} else {
parts = money.split(".");
IntegerNum = parts[0];
DecimalNum = parts[1].substr(0, 4);
}
if (parseInt(IntegerNum, 10) > 0) { //獲取整型部分轉換
var zeroCount = 0;
var IntLen = IntegerNum.length;
for (var i = 0; i < IntLen; i++) {
var n = IntegerNum.substr(i, 1);
var p = IntLen - i - 1;
var q = p / 4;
var m = p % 4;
if (n == "0") {
zeroCount++;
}
else {
if (zeroCount > 0) {
ChineseStr += cnNums[0];
}
zeroCount = 0; //歸零
ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
}
if (m == 0 && zeroCount < 4) {
ChineseStr += cnIntUnits[q];
}
}
ChineseStr += cnIntLast;
//整型部分處理完畢
}
if (DecimalNum != '') { //小數部分
var decLen = DecimalNum.length;
for (var i = 0; i < decLen; i++) {
var n = DecimalNum.substr(i, 1);
if (n != '0') {
ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
}
}
}
if (ChineseStr == '') {
ChineseStr += cnNums[0] + cnIntLast + cnInteger;
} else if (DecimalNum == '') {
ChineseStr += cnInteger;
}
ChineseStr = Symbol +ChineseStr;
return ChineseStr;
}
// alert(changeNumMoneyToChinese(123.11))
let numReg = /^[0-9]*[1-9][0-9]*$/
// let numReg = /^\d+\.\d{0,2}$/;
numReg.test('1.123') //false;
numReg.test('1.13') //true;
numReg.test('1.1') //false;
console.log(numReg.test('1123'))
console.log(numReg.test('1.13'))
console.log(numReg.test('1.1'))
</script>
</body>
</html>