您的位置:首页 > 编程语言 > Java开发

java练习:金额转换,阿拉伯数字转换成中文传统形式

2015-09-05 12:39 656 查看
需求:金额转换,阿拉伯数字转换成中文传统形式 ,例如 101000001010 转为 壹仟零壹拾亿零壹仟零壹拾圆整

最终版:

import java.util.Scanner;
public class Test {
public static void main(String[] args) {
long num = 101000001010l;
String[] digit =	{"零","壹","貳","叁","肆","伍","陆","柒","扒","玖"};
String[] unit = 	{"整","圆","拾","百","仟","万","拾","百","仟","亿",
"拾","百","仟","万"};
while(true){
num = new Scanner(System.in).nextLong();
char[] chArr = Long.toString(num).toCharArray();
StringBuilder sb = new StringBuilder("");
for(int i = 0;i < chArr.length;i++){
String s = digit[Integer.parseInt(String.valueOf(chArr[i]))];
String s2 = unit[chArr.length-i];
sb.append(s).append(s2);
}
sb.append(unit[0]);
String str = sb.toString();
str = change(str);
System.out.println(str);}
}
private static String change(String str) {
String s = str.replaceAll("零[仟百拾]", "零");
s = s.replaceAll("零+", "零").replaceAll("零亿", "亿").replaceAll("零万", "万");
s = s.replaceAll("零圆", "圆").replace("亿万", "亿");
return s;
}
}


有bug版

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;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: