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

java阿拉伯数字表示的金额转换成中文大写金额

2016-11-06 01:16 881 查看
最大数字要处理到千亿
也就是12位
整数部分我们可以分成3段处理,xxxx亿,xxxx万,xxxx元,然后小数部分比较好处理
我们发现0比较难处理
什么时候会出现零呢
那就是两个数字之间出现一个或多个零
那么数字可以分成两类,一类是零,一类是非零
我们只需要统计两个非零数中间有多少个零
没有零那就是没有,有就加上'零'
(非零数用shu这个变量统计,零的数量用ling变量统计)
对于十的读法,一种是十五,十六,另一种是一百一十五,一千零一十五
我们只需要判断十前面有没有其他非零数字就能判断是‘十’还是‘一十’
对于三段,每段的四位数字处理方法都一样,处理完的单位(亿,万,元)要么在非零数结尾要么在
零结尾,在这两个地方都加上判断即可

import java.text.DecimalFormat;

public class NumberUtil {
public static String ToChinese(int Number){
String ans="";
String source=String.valueOf(Number);

return ans;
}

public static String ToBig(int num){
String str[]={"壹","贰","叁","肆","伍","陆","柒","捌","玖","十"};
return str[num-1];
}

public static String Test2(double x){
DecimalFormat format = new DecimalFormat("#.00");
String str = format.format(x);
System.out.println(str);
String s[]=str.split("\\.");
String temp="";
int ling=0;
int shu=0;
int pos=0;
for(int j=0;j<s[0].length();++j){
int num=s[0].charAt(j)-'0';
if(num==0){
ling++;
if(ling==s[0].length()){
temp="零";
}
else if(s[0].length()-j-1==4){
if(shu==1&&(s[0].length()-pos-1)>=5&&(s[0].length()-pos-1)<=7){
temp+="万";
}
}
else if(s[0].length()-j-1==8){
if(shu==1&&(s[0].length()-pos-1)>=9&&(s[0].length()-pos-1)<=11){
temp+="亿";
}
}
}
else{
shu++;
int flag=0;
if(shu==1){
ling=0;
pos=j;
}
if(shu==2){
flag=1;
if(ling>0){
temp+="零";
}
shu=1;
pos=j;
ling=0;
}
if(s[0].length()-j-1==11){
temp+=ToBig(num)+"千";
}
else if(s[0].length()-j-1==10){
temp+=ToBig(num)+"百";
}
else if(s[0].length()-j-1==9){
if(num==1&&flag!=1)
temp+="十";
else
temp+=ToBig(num)+"十";
}
else if(s[0].length()-j-1==8){
temp+=ToBig(num)+"亿";
}
else if(s[0].length()-j-1==7){
temp+=ToBig(num)+"千";
}
else if(s[0].length()-j-1==6){
temp+=ToBig(num)+"百";
}
else if(s[0].length()-j-1==5){
if(num==1&&flag!=1)
temp+="十";
else
temp+=ToBig(num)+"十";
}
else if(s[0].length()-j-1==4){
temp+=ToBig(num)+"万";
}
else if(s[0].length()-j-1==3){
temp+=ToBig(num)+"千";
}
else if(s[0].length()-j-1==2){
temp+=ToBig(num)+"百";
}
else if(s[0].length()-j-1==1){
if(num==1&&flag!=1)
temp+="十";
else
temp+=ToBig(num)+"十";
}
else{
temp+=ToBig(num);
}
}
//            System.out.println(temp);
}
temp+="元";
for(int j=0;j<s[1].length();++j){
int num=s[1].charAt(j)-'0';
if(j==0){
if(num!=0)
temp+=ToBig(num)+"角";
else if(num==0&&1<s[1].length()&&s[1].charAt(1)!='0'){
temp+="零";
}
}
else if(j==1){
if(num!=0)
temp+=ToBig(num)+"分";
}
}
System.out.println(temp);
return temp;
}
public static void main(String[] args) {

Test2(800230001.23);
}

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