java金额大写转小写金额
时间: 2023-09-12 20:05:33 浏览: 117
好的,关于Java金额大写转小写金额的转换方法,我可以给你提供以下代码:
```java
public class MoneyUtil {
private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"};
private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static final String[] CN_UPPER_NUMBER_MONETRAY_UNIT = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "佰", "仟"};
private static final String CN_FULL = "整";
private static final String CN_NEGATIVE = "负";
private static final String CN_ZERO = "零";
private static final double MAX_VALUE = 9999999999999.99D;
public static double convertToDouble(String cnMoney) {
if (cnMoney == null || cnMoney.isEmpty()) {
return 0D;
}
cnMoney = cnMoney.replaceAll("元", "").replaceAll("整", "");
if (cnMoney.startsWith(CN_NEGATIVE)) {
cnMoney = cnMoney.substring(1);
}
double result = 0D;
double temp = 1D;
boolean flag = false;
for (int i = cnMoney.length() - 1; i >= 0; i--) {
int number = getNumber(cnMoney.charAt(i));
if (number < 0) {
if (i == 0) {
result -= temp;
}
flag = true;
continue;
}
if (flag) {
result -= temp;
temp *= 100D;
flag = false;
}
if (number == 10 || number == 11 || number == 12) {
temp = getUnit(number - 10);
} else {
result += temp * number;
}
}
if (cnMoney.startsWith(CN_NEGATIVE)) {
result = -result;
}
if (result < 0 || result > MAX_VALUE) {
return 0D;
}
return result;
}
private static int getNumber(char c) {
for (int i = 0; i < CN_UPPER_NUMBER_MONETRAY_UNIT.length; i++) {
if (CN_UPPER_NUMBER_MONETRAY_UNIT[i].equals(String.valueOf(c))) {
return i;
}
}
return -1;
}
private static double getUnit(int index) {
if (index == 0) {
return 0.1D;
} else if (index == 1) {
return 0.01D;
} else {
return Math.pow(10D, index);
}
}
}
```
这段代码可以将大写金额转换为小写金额。你可以将要转换的大写金额作为参数传递给该方法,然后该方法将返回转换后的小写金额。例如:
```java
String cnMoney = "壹仟贰佰叁拾肆元伍角陆分";
double result = MoneyUtil.convertToDouble(cnMoney);
System.out.println(result);
```
输出结果为:1234.56。
阅读全文