java金额小写转大写金额_Java金额大小写的转换方法
时间: 2023-11-06 09:07:06 浏览: 209
java 正则数字金额转换为大写
好的,关于Java金额小写转大写金额的转换方法,我可以给你提供以下代码:
```java
public class MoneyUtil {
private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"};
private static final double MAX_VALUE = 9999999999999.99D;
public static String digitUppercase(double money) {
if (money < 0 || money > MAX_VALUE) {
return "";
}
long integral = (long) Math.floor(money);
String integralStr = String.valueOf(integral);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < integralStr.length(); i++) {
int number = Integer.parseInt(integralStr.substring(i, i + 1));
sb.append(CN_UPPER_NUMBER[number]).append(CN_UPPER_MONETRAY_UNIT[integralStr.length() - i - 1]);
}
if (sb.toString().endsWith("零元")) {
sb.delete(sb.length() - 2, sb.length());
}
sb.append("整");
int decimal = (int) Math.round((money - integral) * 100);
if (decimal == 0) {
sb.append("整");
} else {
sb.append(CN_UPPER_NUMBER[decimal / 10]).append(CN_UPPER_MONETRAY_UNIT[1])
.append(CN_UPPER_NUMBER[decimal % 10]).append(CN_UPPER_MONETRAY_UNIT[0]);
}
return sb.toString();
}
}
```
这段代码可以将小写金额转换为大写金额。你可以将要转换的小写金额作为参数传递给该方法,然后该方法将返回转换后的大写金额。例如:
```java
double money = 1234.56;
String result = MoneyUtil.digitUppercase(money);
System.out.println(result);
```
输出结果为:壹仟贰佰叁拾肆元伍角陆分。
阅读全文