Java工具類——把金額轉換成漢字大寫金額


[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. class MoneyFormat{    
  2.      private final String [] pattern ={"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};    
  3.      private final String [] cPattern ={"","拾","佰","仟","萬","拾","佰","仟","億"};    
  4.      private final String [] cfPattern = {"","角","分"};    
  5.      private final String ZEOR = "零";    
  6.      public MoneyFormat(){    
  7.          System.out.println("run...");    
  8.           
  9.      }     
  10.          
  11.      public String format(String moneyString){    
  12.       int dotPoint = moneyString.indexOf("."); //判斷是否爲小數    
  13.       String moneyStr;          
  14.       if(dotPoint != -1){    
  15.        moneyStr = moneyString.substring(0,moneyString.indexOf("."));    
  16.       }    
  17.       else{    
  18.        moneyStr = moneyString;    
  19.       }    
  20.       StringBuffer fraction = null;   //小數部分的處理,以及最後的yuan.    
  21.       StringBuffer ms = new StringBuffer();     
  22.       for(int i = 0;i < moneyStr.length();i++){    
  23.        ms.append(pattern[moneyStr.charAt(i) - 48]); //按數組的編號加入對應大寫漢字    
  24.       }    
  25.           
  26.       int cpCursor = 1;    
  27.       for(int j = moneyStr.length() - 1;j > 0;j--){    
  28.        ms.insert(j,cPattern[cpCursor]);   //在j之後加字符,不影響j對原字符串的相對位置    
  29.                   //只是moneyStr.length()不斷增加    
  30.                   //insert(j,"string")就在j位置處插入,j=0時爲第一位    
  31.        cpCursor = cpCursor == 8?1:cpCursor + 1;    //億位之後重新循環    
  32.       }    
  33.           
  34.           
  35.       while(ms.indexOf("零拾") != -1){  //當十位爲零時用一個"零"代替"零拾"    
  36.                 //replace的起始於終止位置    
  37.        ms.replace(ms.indexOf("零拾"),ms.indexOf("零拾") + 2,ZEOR);    
  38.       }    
  39.       while(ms.indexOf("零佰") != -1){  //當百位爲零時,同理    
  40.        ms.replace(ms.indexOf("零佰"),ms.indexOf("零佰") + 2,ZEOR);    
  41.       }    
  42.       while(ms.indexOf("零仟") != -1){  //同理    
  43.        ms.replace(ms.indexOf("零仟"),ms.indexOf("零仟") + 2,ZEOR);    
  44.       }    
  45.       while(ms.indexOf("零萬") != -1){  //萬需保留,中文習慣    
  46.        ms.replace(ms.indexOf("零萬"),ms.indexOf("零萬") + 2,"萬");    
  47.       }    
  48.       while(ms.indexOf("零億") != -1){  //同上    
  49.        ms.replace(ms.indexOf("零億"),ms.indexOf("零億") + 2,"億");    
  50.       }    
  51.       while(ms.indexOf("零零") != -1){//有連續數位出現零,即有以下情況,此時根據習慣保留一個零即可    
  52.        ms.replace(ms.indexOf("零零"),ms.indexOf("零零") + 2,ZEOR);    
  53.       }    
  54.       while(ms.indexOf("億萬") != -1){  //特殊情況,如:100000000,根據習慣保留高位    
  55.        ms.replace(ms.indexOf("億萬"),ms.indexOf("億萬") + 2,"億");    
  56.       }    
  57.       while(ms.lastIndexOf("零") == ms.length()-1){  //當結尾爲零j,不必顯示,經過處理也只可能出現一個零    
  58.           if(ms.indexOf("零") == -1){  
  59.               ms.delete(ms.lastIndexOf("零"),ms.lastIndexOf("零") + 1);    
  60.           }else{  
  61.               break;  
  62.           }  
  63.       }    
  64.           
  65.           
  66.       int end;    
  67.       if((dotPoint = moneyString.indexOf(".")) != -1 ){ //是小數的進入     
  68.        String fs = moneyString.substring(dotPoint + 1,moneyString.length());    
  69.        if(fs.indexOf("00") == -1 || fs.indexOf("00") >= 2){//若前兩位小數全爲零,則跳過操作    
  70.         end = fs.length() > 2?2:fs.length();  //僅保留兩位小數    
  71.         fraction = new StringBuffer(fs.substring(0,end));    
  72.         for(int j = 0;j < fraction.length();j++){    
  73.          fraction.replace(j,j+1,this.pattern[fraction.charAt(j) - 48]); //替換大寫漢字    
  74.         }    
  75.         for(int i = fraction.length();i > 0;i--){  //插入中文標識    
  76.          fraction.insert(i,cfPattern[i]);    
  77.         }    
  78.         fraction.insert(0,"元");      //爲整數部分添加標識    
  79.        }    
  80.        else{    
  81.         fraction = new StringBuffer("元整");     
  82.        }    
  83.            
  84.       }    
  85.       else{    
  86.        fraction = new StringBuffer("元整");    
  87.       }    
  88.            
  89.       ms.append(fraction);         //加入小數部分    
  90.       return ms.toString();    
  91.      }    
  92.          
  93.          
  94.          
  95.          
  96.      public static void main(String [] ar){    
  97.       System.out.println(new MoneyFormat().format("10005022.123009"));    
  98.       System.out.println(new MoneyFormat().format("0.12"));    
  99.      }    
  100.     }     

轉自【http://www.open-open.com/lib/view/open1400206687504.html】
發佈了46 篇原創文章 · 獲贊 5 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章