笑看風(fēng)云

          一切從頭開(kāi)始
          posts - 28, comments - 1, trackbacks - 0, articles - 2
                  最近在做開(kāi)發(fā)的時(shí)候,有一部分功能要實(shí)現(xiàn)把數(shù)字轉(zhuǎn)換成中文貨幣表示并顯示給客戶端,想了好久不知道如何寫(xiě),到google上搜了一下,結(jié)果這方面的資料還比較多,看了一下別人寫(xiě)的,感覺(jué)很不錯(cuò),就把它復(fù)制過(guò)來(lái),根據(jù)實(shí)際需要改了一下,已經(jīng)測(cè)試成功了.代碼如下:
          package test;

          import java.math.BigDecimal;

          public class StrangeCurrency {
          /**
          * Description 將數(shù)字金額轉(zhuǎn)換為中文金額

          */

          public static String DoNumberCurrencyToChineseCurrency(
              
          double bigdMoneyNumber) {
             
          // 中文金額單位數(shù)組
             String[] straChineseUnit = """""""""""""萬(wàn)""",
               
          """""""""""" }
          ;
             
          // 中文數(shù)字字符數(shù)組
             String[] straChineseNumber = """""""""""""""",
               
          """" }
          ;
             String strChineseCurrency 
          = "";
             
          // 零數(shù)位標(biāo)記
             boolean bZero = true;
             
          // 中文金額單位下標(biāo)
             int ChineseUnitIndex = 0;
             
          // DoPrintMessage("Calling Method DoNumberCurrencyToChineseCurrency");
             try {
              
          if (bigdMoneyNumber == 0)
               
          return "零圓整";
              
          // 處理小數(shù)部分,四舍五入
              double doubMoneyNumber = Math.round(bigdMoneyNumber * 100);
              
          // 是否負(fù)數(shù)
              boolean bNegative = doubMoneyNumber < 0;
              
          // 取絕對(duì)值
              doubMoneyNumber = Math.abs(doubMoneyNumber);
              
          // 循環(huán)處理轉(zhuǎn)換操作
              while (doubMoneyNumber > 0{
               
          // 整的處理(無(wú)小數(shù)位)
               if (ChineseUnitIndex == 2 && strChineseCurrency.length() == 0)
                strChineseCurrency 
          = strChineseCurrency + "";
               
          // 非零數(shù)位的處理
               if (doubMoneyNumber % 10 > 0{
                strChineseCurrency 
          = straChineseNumber[(int) doubMoneyNumber % 10]
                  
          + straChineseUnit[ChineseUnitIndex]
                  
          + strChineseCurrency;
                bZero 
          = false;
               }

               
          // 零數(shù)位的處理
               else {
                
          // 元的處理(個(gè)位)
                if (ChineseUnitIndex == 2{
                 
          // 段中有數(shù)字
                 if (doubMoneyNumber > 0{
                  strChineseCurrency 
          = straChineseUnit[ChineseUnitIndex]
                    
          + strChineseCurrency;
                  bZero 
          = true;
                 }

                }

                
          // 萬(wàn)、億數(shù)位的處理
                else if (ChineseUnitIndex == 6 || ChineseUnitIndex == 10{
                 
          // 段中有數(shù)字
                 if (doubMoneyNumber % 1000 > 0)
                  strChineseCurrency 
          = straChineseUnit[ChineseUnitIndex]
                    
          + strChineseCurrency;
                }

                
          // 前一數(shù)位非零的處理
                if (!bZero)
                 strChineseCurrency 
          = straChineseNumber[0]
                   
          + strChineseCurrency;
                bZero 
          = true;
               }

               doubMoneyNumber 
          = Math.floor(doubMoneyNumber / 10);
               ChineseUnitIndex
          ++;
              }

              
          // 負(fù)數(shù)的處理
              if (bNegative)
               strChineseCurrency 
          = "負(fù)" + strChineseCurrency;
             }
           catch (Exception e) {
              
          // DoPrintMessage("Exception Reason : Call Method
              
          // DoNumberCurrencyToChineseCurrency Error");
              
          // DoPrintMessage("Exception Message : " + e.getMessage());
              e.printStackTrace();
              
          return "";
             }

             
          return strChineseCurrency;
          }



          /**
          * @param args
          */
          //測(cè)試代碼
          public static void main(String[] args) {
             double rmb=23434.89;
             StrangeCurrency sc=new StrangeCurrency();
             String s=sc.DoNumberCurrencyToChineseCurrency(rmb);
              System.out.println(s);
             }

          }

          Feedback

          # re: 把數(shù)字轉(zhuǎn)換成中文貨幣表示  回復(fù)  更多評(píng)論   

          2010-09-03 10:09 by 笨牛
          10001000.09 竟然輸出
          壹仟零壹仟圓零玖分
          主站蜘蛛池模板: 福清市| 康马县| 成武县| 新巴尔虎左旗| 海淀区| 曲沃县| 广元市| 卢湾区| 高州市| 疏勒县| 石台县| 莱州市| 云南省| 府谷县| 淳化县| 黄龙县| 金沙县| 贵南县| 红河县| 兴仁县| 安化县| 仁布县| 苍溪县| 瑞昌市| 山西省| 秦皇岛市| 黄浦区| 定西市| 湄潭县| 锡林浩特市| 淮南市| 凤庆县| 繁峙县| 光山县| 墨竹工卡县| 呼伦贝尔市| 榆中县| 洛扎县| 区。| 读书| 朝阳市|