那位懂java网友可以帮帮我啊
零、壹、贰、叁、肆、伍、陆、柒、捌、玖亿、仟、佰、拾、万、仟、佰、拾、元、角...
例如:1234.12转换为人民币大写为:壹仟贰佰叁拾肆元壹角贰分
要求:
1、 可将阿拉伯数字的金额转换为中文格式的金额。
2、 完成整数部分的转换。
3、 可对小写部分进行转换。
这个怎么做啊
好的话加分
追问:高手啊,你QQ多少啊, 厉害厉害 ………………
亿、仟、佰、拾、万、仟、佰、拾、元、角...
例如:1234.12转换为人民币大写为:壹仟贰佰叁拾肆元壹角贰分
要求:
1、 可将阿拉伯数字的金额转换为中文格式的金额。
2、 完成整数部分的转换。
3、 可对小写部分进行转换。
这个怎么做啊
好的话加分
追问:高手啊,你QQ多少啊, 厉害厉害 ………………
答案:
键盘输入数字后后直接显示大写的中文金额
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tra {
private static final Pattern AMOUNT_PATTERN =
Pattern.compile("^(0|[1-9]\\d{0,11})\\.(\\d\\d)$"); // 不考虑分隔符的正确性
private static final char[] RMB_NUMS = "零壹贰叁肆伍陆柒捌玖".toCharArray();
private static final String[] UNITS = {"元", "角", "分", "整"};
private static final String[] U1 = {"", "拾", "佰", "仟"};
private static final String[] U2 = {"", "万", "亿"};
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入金额(如1234.12):");
String a = s.next();
System.out.println(convert(a));
}
public static String convert(String amount) throws IllegalArgumentException {
// 去掉分隔符
amount = amount.replace(",", "");
// 验证金额正确性
if (amount.equals("0.00")) {
throw new IllegalArgumentException("金额不能为零.");
}
Matcher matcher = AMOUNT_PATTERN.matcher(amount);
if (! matcher.find()) {
throw new IllegalArgumentException("输入金额有误.");
}
String integer = matcher.group(1); // 整数部分
String fraction = matcher.group(2); // 小数部分
String result = "";
if (! integer.equals("0")) {
result += integer2rmb(integer) + UNITS[0]; // 整数部分
}
if (fraction.equals("00")) {
result += UNITS[3]; // 添加[整]
} else if (fraction.startsWith("0") && integer.equals("0")) {
result += fraction2rmb(fraction).substring(1); // 去掉分前面的[零]
} else {
result += fraction2rmb(fraction); // 小数部分
}
return result;
}
// 将金额小数部分转换为中文大写
private static String fraction2rmb(String fraction) {
char jiao = fraction.charAt(0); // 角
char fen = fraction.charAt(1); // 分
return (RMB_NUMS[jiao - '0'] + (jiao > '0' ? UNITS[1] : ""))
+ (fen > '0' ? RMB_NUMS[fen - '0'] + UNITS[2] : "");
}
// 将金额整数部分转换为中文大写
private static String integer2rmb(String integer) {
StringBuilder buffer = new StringBuilder();
// 从个位数开始转换
int i, j;
for (i = integer.length() - 1, j = 0; i >= 0; i--, j++) {
char n = integer.charAt(i);
if (n == '0') {
// 当n是0且n的右边一位不是0时,插入[零]
if (i < integer.length() - 1 && integer.charAt(i + 1) != '0') {
buffer.append(RMB_NUMS[0]);
}
// 插入[万]或者[亿]
if (j % 4 == 0) {
if (i > 0 && integer.charAt(i - 1) != '0'
|| i > 1 && integer.charAt(i - 2) != '0'
|| i > 2 && integer.charAt(i - 3) != '0') {
buffer.append(U2[j / 4]);
}
}
} else {
if (j % 4 == 0) {
buffer.append(U2[j / 4]); // 插入[万]或者[亿]
}
buffer.append(U1[j % 4]); // 插入[拾]、[佰]或[仟]
buffer.append(RMB_NUMS[n - '0']); // 插入数字
}
}
return buffer.reverse().toString();
}
}