黑马程序员技术交流社区
标题:
练习:金额转换,阿拉伯数字转换成中文传统形式
[打印本页]
作者:
fmi110
时间:
2015-9-5 12:33
标题:
练习:金额转换,阿拉伯数字转换成中文传统形式
a
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long num = 101000001010l;//一千零一十亿零一千零一十圆整
num = 1010l;//一千零一十亿零一千零一十圆整
while(true){
System.out.println("输入数学数字:");
num = new Scanner(System.in).nextLong();
String[] unit = {"圆整","万","亿"};
int count = -1;
StringBuilder sb = new StringBuilder("");
while(num>0){
count++;
String s = "";
long n = num%10000;//取后四位
if(n < 10)
s = "000"+n;
else if(n < 100 )
s = "00"+n;
else if(n < 1000)
s = "0"+n;
else
s = s+n;
num /= 10000;
if(!s.equals("0000"))
s = toNum(s)+unit[count];//返回的数字加单位
else
s = toNum(s);
sb.insert(0, s);
// System.out.println(sb);
}
String chineseNum = sb.toString();
System.out.println(chineseNum);
}
}
public static String toNum(String num){
String[] digit = {"零","壹","貳","叁","肆","伍","陆","柒","扒","玖"};
char[] ch = num.toCharArray();//字符串形式的四位数
int count = 0;//记录字符串含0的个数
int index = -1;
while((index = num.indexOf("0",index+1))!= -1)
count++;
// System.out.println("字符串含0个数count = "+count);
String numStr = "";//返回的中文数字
switch(count){
case 4:
numStr = "零";
break;
case 3:
int temp = Integer.parseInt(num);
if(temp<10)
numStr = digit[ch[3]-'0'];
else if(temp > 9 && temp < 101)
numStr = digit[ch[2]-'0']+"拾";
else if(temp > 99 && temp < 1000)
numStr = digit[ch[1]-'0'] + "百";
else
numStr = digit[ch[0]-'0'] + "仟";
break;
case 2:
if(num.matches("[0][0][1-9][1-9]")){
numStr = numStr + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];;
}
else if(num.matches("[1-9][0][0][1-9]")){
numStr = numStr + digit[ch[0]-'0'] + "仟" +"零"+ digit[ch[3]-'0'];
}
else if(num.matches("[1-9][1-9][0][0]")){
numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百";
}
else if(num.matches("[0][1-9][1-9][0]")){
numStr = numStr + digit[ch[1]-'0'] + "百"+ digit[ch[2]-'0']+"拾";
}
else if(num.matches("[0][1-9][0][1-9]")){
numStr = numStr + digit[ch[1]-'0'] + "百"+"零"+ digit[ch[3]-'0'];
}
else if(num.matches("[1-9][0][1-9][0]")){
numStr = numStr + digit[ch[0]-'0'] + "仟"+"零"+ digit[ch[2]-'0']+"拾";
}
break;
case 1:
if(num.matches("[0][1-9][1-9][1-9]")){
numStr = numStr + digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];
}
else if(num.matches("[1-9][0][1-9][1-9]")){
numStr = numStr + digit[ch[0]-'0'] + "仟" +"零"+ digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];
}
else if(num.matches("[1-9][1-9][0][1-9]")){
numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + "零"+digit[ch[3]-'0'];
}
else if(num.matches("[1-9][1-9][1-9][0]")){
numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾";
}
break;
default:
numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];
}
// System.out.println(numStr);
return numStr;
}
}
复制代码
作者:
fmi110
时间:
2015-9-5 12:34
运行结果:
输入数学数字:
1
壹圆整
输入数学数字:
12
壹拾貳圆整
输入数学数字:
1003
壹仟零叁圆整
输入数学数字:
101000001010
壹仟零壹拾亿零壹仟零壹拾圆整
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2