项目中遇到货币大小写的转换问题
1.代码在util文件夹中新建一个changeMoney.js
文件
2.在需要用的地址引入文件import changeMoney from "@/util/changeMoney";
3.直接用即可changeMoney.moneyToNumber(this.customIptW);
let RMB_NUMBER = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
let RMB_START = ["元", "圆"];
let RMB_DEC = ["角", "分"];
let RMB_ZHENG = "整";
let RMB_YUAN = "元";
let RMB_WAN = "万";
let RMB_YI = "亿";
let RMB_SHI = "拾";
let RMB_BAI = "佰";
let RMB_QIAN = "仟";
let RMB_RIGHT = [RMB_YUAN, RMB_WAN, RMB_YI];
let RMB_WEIGHT = [RMB_SHI, RMB_BAI, RMB_QIAN];
export default {
getNumber(ch) {
for (let i = 0; i < RMB_NUMBER.length; i++) {
if (ch == RMB_NUMBER[i]) {
return i;
}
}
return -1;
},
getWeight(ch) {
switch (ch) {
case RMB_SHI:
return 10;
case RMB_BAI:
return 100;
case RMB_QIAN:
return 1000;
}
return -1;
},
getRight(ch) {
switch (ch) {
case RMB_YUAN:
return 1;
case RMB_WAN:
return 10000;
case RMB_YI:
return 100000000;
}
return -1;
},
//大写转小写
moneyToNumber(money) {
let right = 1;
let weight = 1;
let sum = 0;
let index = money.lastIndexOf(RMB_START[0]);
if (index == -1) {
index = money.lastIndexOf(RMB_START[1]);
if (index == -1) {
return "Error";
}
}
//处理整数部分
for (let i = index; i >= 0; i--) {
let ch = money.substr(i, 1);
let temp = -1;
if ((temp = this.getWeight(ch)) != -1) {
weight = temp;
} else if ((temp = this.getRight(ch)) != -1) {
right = temp;
weight = 1;
} else if ((temp = this.getNumber(ch)) != -1) {
sum += temp * weight * right;
}
}
//处理部分省略掉开头“壹”情况
if ((weight = this.getWeight(money.substr(0, 1))) != -1) {
sum += weight * right;
}
//处理小数部分
let str = "";
for (let i = index; i < money.length; i++) {
let line;
if ((line = this.getNumber(money.substr(i, 1))) != -1) {
str += line;
}
}
if (str.length != 0) {
str = "." + str;
}
return sum + str;
},
// ConvertToChinese(number)
// {
// let s = number.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
// let d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
// let r = Regex.Replace(d, ".", m => "负圆空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString());
// return r;
// }
//小写转大写(恰好十万的时候会少一个万)
moneyToChinese(n) {
let UPPER_NUM = [
"零",
"壹",
"贰",
"叁",
"肆",
"伍",
"陆",
"柒",
"捌",
"玖"
];
let MAX_UNIT = [
"圆",
"拾",
"佰",
"仟",
"万",
"拾",
"佰",
"仟",
"亿",
"拾",
"佰",
"仟"
];
let MIN_UNIT = ["角", "分"];
let _html = "",
integer = n,
fract;
if (n.indexOf(".") != -1) {
integer = n.split(".")[0];
fract = n.split(".")[1];
}
for (let i = 0, j = integer.length, len = j; i < len; i++) {
let _index = integer.charAt(i);
--j;
_html += UPPER_NUM[_index] + (_index > 0 ? MAX_UNIT[j] : "");
}
_html = _html.replace(/零{2,}/g, "零").replace(/零$/, "元");
if (fract !== undefined) {
for (let i = 0, j = 0, len = fract.length; i < len; i++) {
_html += UPPER_NUM[fract.charAt(i)] + MIN_UNIT[j++];
}
} else {
_html += "整";
}
return _html;
},
//超过一千亿不管用
smalltoBIG(n) {
let fraction = ["角", "分"];
let digit = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
let unit = [
["元", "万", "亿"],
["", "拾", "佰", "仟"]
];
let head = n < 0 ? "欠" : "";
n = Math.abs(n);
let s = "";
for (let i = 0; i < fraction.length; i++) {
s += (
digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]
).replace(/零./, "");
}
s = s || "整";
n = Math.floor(n);
for (let i = 0; i < unit[0].length && n > 0; i++) {
let p = "";
for (let j = 0; j < unit[1].length && n > 0; j++) {
p = digit[n % 10] + unit[1][j] + p;
n = Math.floor(n / 10);
}
s = p.replace(/(零.)*零$/, "").replace(/^$/, "零") + unit[0][i] + s;
}
return (
head +
s
.replace(/(零.)*零元/, "元")
.replace(/(零.)+/g, "零")
.replace(/^整$/, "零元整")
);
}
};