public class Digitizer {
/*
* 12345
* 壹万贰仟叁佰肆拾伍圆
*/
private static char[] numArr = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
private static char[] unitArr = {'圆','拾','佰','仟','万','拾','佰','仟','亿','拾','佰','仟'};
public static void main(String[] args) {
System.out.println(toChinese(12345)); //将数字转换成对应的中文
System.out.println(toChinese(0));
System.out.println(toChinese(10000));
System.out.println(toChinese(1000000001));
}
private static String toChinese(int num) {
StringBuilder sb = new StringBuilder();
if(num == 0) {
return "零圆";
}
for(int i = 0; num > 0; i++) {
int temp = num % 10; //获取num的每一个数字
num = num / 10; //每获取一位后就将数字去掉一位
sb.insert(0,unitArr[i]); //向0角标的位置添加单位
sb.insert(0, numArr[temp]); //向0角标的位置添加数字文字
}
return sb.toString()
.replaceAll("零[仟佰拾]", "零")
.replaceAll("零+", "零")
.replaceAll("零([亿万])", "$1")
.replace("亿万", "亿")
.replace("零圆", "圆");
}
}
|
|