本人比较懒,未加注释,有什么不懂的,到群里@我,群名片叫会飞的菜鸟。
- import java.util.*;
- public class ChineseOrNumber
- {
- public static void main(String[] args)
- {
- // 把八万四千二百三十二点五八转数字 84232.58
- sop(Integer.MAX_VALUE+toChineseNum(Integer.MAX_VALUE+""));
- sop(Long.MAX_VALUE+toChineseNum(Long.MAX_VALUE+""));
- for(long i=10000L;i<900000000000000L;i+=500050005L){
- System.out.print(i+":");
- String temp=toChineseNum(i+"");
- String temp2=toNum(temp);
- sop(temp);
- sop(temp2);
- if(!temp2.equals(i+"")){
- sop("错误");
- break;
- }
- }
- }
- public static String toNum(String str){
- HashMap<String,Integer> number=new HashMap<String,Integer>();
- number.put("一",1);
- number.put("二",2);
- number.put("三",3);
- number.put("四",4);
- number.put("五",5);
- number.put("六",6);
- number.put("七",7);
- number.put("八",8);
- number.put("九",9);
- String zhengshu,xiaoshu;
- if(str.indexOf('点')!=-1){
- String[] shu=str.split("点");
- zhengshu=shu[0];
- xiaoshu=shu[1];
- }else{
- zhengshu=str;
- xiaoshu="0";
- }
- zhengshu=toNumPrivate(zhengshu,number);
- while(zhengshu.charAt(0)=='0')
- zhengshu=zhengshu.substring(1,zhengshu.length());
- number.put("零",0);
- if(xiaoshu.equals("0"))
- return zhengshu;
- else
- return zhengshu+"."+toXiaoshu(xiaoshu,number);
- }
- private static String toNumPrivate(String str,HashMap<String,Integer> number){
- String[] sheet={"亿亿","亿","万"};
- int[] numSheet={16,8,4};
- for(int i=0;i<sheet.length;i++){
- if(str.contains(sheet[i])){
- String[] strArr=str.split(sheet[i]);
- String right;
- if(strArr.length==1)
- right=getRepeatZero(numSheet[i]);
- else
- right=toNumPrivate(strArr[1],number);
- right=getRepeatZero(numSheet[i]-right.length())+right;
- return toNumPrivate(strArr[0],number)+right;
- }
- }
- return toZhengshu(str,number);
- }
- public static String getRepeatZero(int num){
- StringBuilder sb=new StringBuilder();
- for(int i=0;i<num;i++)
- sb.append("0");
- return sb.toString();
- }
- private static String toXiaoshu(String str,HashMap<String,Integer> number){
- StringBuilder resultStr=new StringBuilder();
- char[] ch=str.toCharArray();
- Integer temp=null;
- // 把八万四千二百三十二点五八转数字 84232.58
- for(char c:ch)
- resultStr.append(number.get(c+""));
- return resultStr.toString();
- }
- private static String toZhengshu(String str,HashMap<String,Integer> number){
- HashMap<String,Integer> wei=new HashMap<String,Integer>();
- /*相应位所占权重*/
- wei.put("千",1000);
- wei.put("百",100);
- wei.put("十",10);
- int radix=0,tempResult=0,result=0;
- String resultStr=null;
- char[] ch=str.toCharArray();
- Integer temp=null;
- // 把八万四千二百三十二点五八转数字 84232.58
- for(char c:ch){
- if((temp=number.get(c+""))!=null){
- result+=tempResult;
- tempResult=0;
- radix=temp.intValue();
- }else if((temp=wei.get(c+""))!=null){
- tempResult=(tempResult==0)?radix*temp:tempResult*temp;
- }
- }
- result+=(tempResult==0)?radix:tempResult;
- resultStr=result+"";
- return getRepeatZero(4-resultStr.length())+resultStr;
- }
- public static String toChineseNum(String numStr){
- char[] numSheet={'零','一','二','三','四','五','六','七','八','九'};
- String zhengshu,xiaoshu;
- if(numStr.indexOf(".")!=-1){
- String[] shu=numStr.split("\\.");
- zhengshu=shu[0];
- xiaoshu=shu[1];
- }else{
- zhengshu=numStr;
- xiaoshu="0";
- }
- zhengshu=zhengshu.replaceAll("\\D","");
- xiaoshu=xiaoshu.replaceAll("\\D","");
- String result=toChineseNum(zhengshu,numSheet);
- if(result.equals(""))
- return "转换失败,数值过大或者参数有误";
- //最终结果头部去零
- while(result.charAt(0)=='零')
- result=result.substring(1,result.length());
- if(xiaoshu.equals("0"))
- return result;
- else
- return result+"点"+toChinesexiaoshu(xiaoshu,numSheet);
- }
- private static String toChinesexiaoshu (String num,char[] numSheet){
- StringBuilder result=new StringBuilder();
- for(int i=0;i<num.length();i++)
- result.append(numSheet[Integer.parseInt(num.charAt(i)+"")]);
- return result.toString();
- }
- private static String toChineseNum (String num,char[] numSheet){
- /*-----------对小于一万的数据进行处理,------------*/
- if(num.length()<=4) {
- String result="";
- char[] UnitArr={'千','百','十'};
- num=getRepeatZero(4-num.length())+num;//对不足四位的数前面补零,直到变成4位
- //遍历4位数的4个数字
- for(int i=0;i<=3;i++) {
- int temp=Integer.parseInt(num.charAt(i)+"");
- /*
- 分为当前位等于0和不等于0的情况
- 当当前位等于0时,如果是千位就直接赋值零
- 如果不是千位,则判断前面一位是否为零,不是的话就赋值
- */
- if(temp!=0){
- result+=numSheet[temp];
- if(i!=3) result+=UnitArr[i];
- }else{//当前位为零
- if(i==0){
- result+="零";
- }else{
- temp=Integer.parseInt(num.charAt(i-1)+"");
- if(temp!=0) result+="零";
- }
- }
- }
- //清楚尾部的零
- if(result.charAt(result.length()-1)=='零')
- result=result.substring(0,result.length()-1);
- return result;//可能返回"零*","*",""(*代表头部和尾部不含零的大写数字)
- }
- /*-----------对大于等于一万小于一亿的数据进行处理------------*/
- if(num.length()<=8 && num.length()>4){
- String left=toChineseNum(num.substring(0,num.length()-4),numSheet);
- String right=toChineseNum(num.substring(num.length()-4,num.length()),numSheet);
- return compara(left,right,"万");
- }
- /*-----------对大于等于一亿小于一亿亿的数据进行处理------------*/
- if(num.length()<=16 && num.length()>8){
- String left=toChineseNum(num.substring(0,num.length()-8),numSheet);
- String right=toChineseNum(num.substring(num.length()-8,num.length()),numSheet);
- return compara(left,right,"亿");
- }
- /*-----------2014年国民生产总值也不到一亿亿,再高的就不处理了------------*/
- if(num.length()<=32 && num.length()>16){
- String left=toChineseNum(num.substring(0,num.length()-16),numSheet);
- String right=toChineseNum(num.substring(num.length()-16,num.length()),numSheet);
- return compara(left,right,"亿亿");
- }
- return "";
- }
- //可能返回"零*","*",""(*代表头部和尾部不含零的大写数字)
- public static String compara(String left,String right,String connector){
- if(left.equals("")){
- if((!right.equals("") && right.charAt(0)=='零') || right.equals(""))
- return right;
- else
- return "零"+right;
- }
- return left+connector+right;
- }
- public static void sop(Object o){
- System.out.println(o);
- }
- }
复制代码
|