java人民币大小写转换
时间: 2024-01-17 12:19:26 浏览: 73
java实现将数字转换成人民币大写
以下是一个Java程序,用于将人民币小写金额转换为大写金额:
```java
import java.text.NumberFormat;
public class RMBConverter {
public static void main(String[] args) {
double num = 20100104600.01;
String s = upperCase2(num);
System.out.println("大写金额:" + s);
}
public static String upperCase2(double num) {
String[] fraction = {"角", "分"};
String[] digit = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String[][] unit = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};
String head = num < 0 ? "负" : "";
num = Math.abs(num);
String s = "";
for (int i = 0; i < fraction.length; i++) {
s += (digit[(int) (Math.floor(num * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
}
if (s.length() < 1) {
s = "整";
}
int integerPart = (int) Math.floor(num);
for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
String p = "";
for (int j = 0; j < unit[1].length && num > 0; j++) {
p = digit[integerPart % 10] + unit[1][j] + p;
integerPart /= 10;
}
s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
}
return head + s.replaceAll("(零.)*零元", "元").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
}
}
```
运行以上代码,将会输出人民币小写金额20100104600.01的大写金额为贰佰零壹亿零壹佰万肆仟陆佰元零壹分。
阅读全文