最近,在一个银行项目中接触到把数字向人民币大写转换的问题。其实也并不难,我们需要一个方法把数字分割成个位,十位,百位等进行替换。
下面是一个实现两位数的数字向人民币大写方式的转换案例:
public Object execute(Object[] args) throws Exception {
int amout = ((Integer)args[0]).intValue();
String[] str = { "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" };
String result = "";
if (amout <= 10) {
result = str[(amout - 1)] + "元";
} else {
int ten = amout / 10;
int gewei = amout % 10;
String shiWei = "";
if (ten == 1)
shiWei = "拾";
else {
shiWei = str[(ten - 1)] + "拾";
}
if (gewei == 0)
result = shiWei + "元";
else {
result = shiWei + str[(gewei - 1)] + "元";
}
}
System.out.println(result);
return result;
}
在main方法中,进行调用
public static void main(String[] args) throws Exception {
Question1 a = new Question1();
a.execute(new Object[] { Integer.valueOf(4) });
a.execute(new Object[] { Integer.valueOf(10) });
a.execute(new Object[] { Integer.valueOf(11) });
a.execute(new Object[] { Integer.valueOf(25) });
a.execute(new Object[] { Integer.valueOf(89) });
a.execute(new Object[] { Integer.valueOf(90) });
}