数字转换字符串

将一个字符串转换为我们中国人读的方式!要求:
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());  
        }
 
    }
}
一亿点一二
一千万零一点四五
二百四十万零一点零一
四千五百九十七万四千六百二十三点八零
八百九十万零五百四十二点七八
零点九九二
二十万零三千二百点九九二


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值