数字转换字符串

来源:互联网 发布:v5shop微信分销源码 编辑:程序博客网 时间:2024/06/05 07:23
将一个字符串转换为我们中国人读的方式!要求:
1.该字符串只能含数字和小数点(要检测是否含有其他非法字符或为空);
2.要考虑到很多的情况(比如):
         a.100 000 000.12(一亿点一二);
         b.10 000 001.45(一千万零一点四五);
         c.2 400 001.01(二百四十万零一点零一);
         d.45 974 623.80(四千五百九十七万四千六百二十三点八零);

         e.8 900 542.78(八百九十万零五百四十二点七八);

import java.util.HashMap;import java.util.LinkedList;import java.util.Map; public class Test {     private Map<String , String> e2c = new HashMap<String, String>();    {        e2c.put("0", "零");        e2c.put("1", "一");        e2c.put("2", "二");        e2c.put("3", "三");        e2c.put("4", "四");        e2c.put("5", "五");        e2c.put("6", "六");        e2c.put("7", "七");        e2c.put("8", "八");        e2c.put("9", "九");    }     private String[] ws = new String[]{" " ,"十" , "百" ,"千" ,"万" , "十万" ,"百万" ,"千万" ,"亿" ,"十亿" ,"百亿" ,"千亿"};     private String str ;     //要转换的字符串     private LinkedList<String> beforePoint ;   //保存小数点之前的内容     private String afterPoint ;  //保存小数点之后的内容     public Test(String str){        this.str = str ;        String[] tmp = str.split("\\.");        beforePoint = new LinkedList<String>();        char[] cs = tmp[0].toCharArray();        for (Character c : cs) {              beforePoint.add(String.valueOf(c));          }        if(tmp.length > 1)            afterPoint = tmp[1] ;    }     private String translate(){        int currentIndex = 0 ;          StringBuilder sb = new StringBuilder();        String lastNumber = "0";        String lastFh = " ";        while(!beforePoint.isEmpty()){            String s = beforePoint.removeLast();            String fh =  ws[currentIndex] ;            if( lastNumber.equals("0") && s.equals("0")){                if(beforePoint.isEmpty())                    sb.insert(0, e2c.get(s));             }else {                sb.insert(0, e2c.get(s));                  if(!s.equals("0")){                    if(lastFh.charAt(lastFh.length() - 1) == fh.charAt(fh.length() - 1))                         fh = fh.substring(0 , fh.length() - 1);                    sb.insert( 1, fh);                }            }            lastNumber = s;            lastFh = s.equals("0") ? " " : ws[currentIndex];            currentIndex++;        }        return sb.toString();    }     private String format(){        String before = translate();        StringBuilder after = new StringBuilder();        if(afterPoint != null){            before += "点";            for (int i = 0; i < afterPoint.length() ; i++) {                after.append(e2c.get(String.valueOf(afterPoint.charAt(i))));            }        }        return before + after.toString();    }     public static void main(String[] args) {         String [] ss = new String[]{                "100000000.12"                ,"10000001.45"                ,"2400001.01"                ,"45974623.80"                ,"8900542.78"                ,"0.992"                ,"203200.992"        };         for (String s : ss) {            System.out.println(new Test(s).format());          }     }}
一亿点一二一千万零一点四五二百四十万零一点零一四千五百九十七万四千六百二十三点八零八百九十万零五百四十二点七八零点九九二二十万零三千二百点九九二


原创粉丝点击