posts - 14,  comments - 37,  trackbacks - 0

采用了递归的方法

  1package src;
  2/** *//**
  3 * 
  4 * @author Mr.Yang
  6 */

  7public class Numeric {
  8
  9    /** *//**
 10     * @param args
 11     */

 12    public static void main(String[] args) {
 13        System.out.println(getChinese(2147483648l,0));
 14    }

 15    /** *//**
 16     * 获得阿拉伯数字对应的中文
 17     * 最大只支持到9千9百九十九亿9千9百九十九万9千9百九十九
 18     * @param number 要转换的数字
 19     * @param depth 递归深度,使用时候直接给0即可
 20     * @return 数字的中文描述
 21     */

 22    public static String getChinese(long number,int depth){
 23        if(depth<0)
 24            depth = 0;
 25        String chinese = "";
 26        String src = number+"";
 27        if(src.charAt(src.length()-1)=='l' || src.charAt(src.length()-1)=='L' )
 28        {
 29            src = src.substring(0, src.length()-1);
 30        }

 31        System.out.println(src);
 32        
 33        if(src.length()>4)
 34            chinese = getChinese(Integer.parseInt(src.substring(0, src.length()-4)),depth+1)+
 35                getChinese(Integer.parseInt(src.substring(src.length()-4, src.length())),depth-1);
 36        else{
 37            char prv = 0;
 38            for(int i=0;i<src.length();i++){
 39                switch(src.charAt(i)){
 40                case '0':
 41                    if(prv != '0')
 42                    chinese = chinese+"";
 43                    break;
 44                case '1':
 45                    chinese = chinese+"";
 46                    break;
 47                case '2':
 48                    chinese = chinese+"";
 49                    break;
 50                case '3':
 51                    chinese = chinese+"";
 52                    break;
 53                case '4':
 54                    chinese = chinese+"";
 55                    break;
 56                case '5':
 57                    chinese = chinese+"";
 58                    break;
 59                case '6':
 60                    chinese = chinese+"";
 61                    break;
 62                case '7':
 63                    chinese = chinese+"";
 64                    break;
 65                case '8':
 66                    chinese = chinese+"";
 67                    break;
 68                case '9':
 69                    chinese = chinese+"";
 70                    break;
 71                }

 72                prv = src.charAt(i);
 73                
 74                switch(src.length()-1-i){
 75                case 1://
 76                    if(prv != '0')
 77                    chinese = chinese + "";
 78                    break;
 79                case 2://
 80                    if(prv != '0')
 81                    chinese = chinese + "";
 82                    break;
 83                case 3://
 84                    if(prv != '0')
 85                    chinese = chinese + "";
 86                    break;
 87                
 88                }

 89            }

 90        }

 91        while(chinese.length()>0 && chinese.lastIndexOf("")==chinese.length()-1)
 92            chinese = chinese.substring(0,chinese.length()-1);
 93        if(depth == 1)
 94            chinese += "";
 95        if(depth == 2)
 96            chinese += "亿";
 97        
 98        return chinese;
 99    }

100
101}

102
posted on 2007-07-09 15:46 冰封的爱 阅读(1079) 评论(1)  编辑  收藏 所属分类: 常用工具

FeedBack:
# re: 阿拉伯数字转换为中文大写汉字
2009-01-30 18:31 | 往往
我是我  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(3)

随笔档案

文章分类

文章档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜