[收藏]JAVA中金額的中文大寫方式

          Posted on 2006-04-02 15:34 ikingqu 閱讀(713) 評論(1)  編輯  收藏 所屬分類: JavaSE&JavaEE

          題意:

          讀入一個浮點(diǎn)數(shù)值,將其轉(zhuǎn)化為中文金額的大寫方式.

          試驗(yàn)要求:

          當(dāng)金額為整數(shù)時,只表示整數(shù)部分,省略小數(shù)部分,并添加"整"字.

          當(dāng)金額中含有連續(xù)的0時,只需要一個"零"即可.

          10的表示方式.例如110--壹佰一拾元整,10---一拾元整

          ? 1 import java.io. * ;
          ? 2 class chineseMoney {
          ? 3 ??? private String number[] = { "" , " " , " " , " " , " " , " " , " " , " " , " " , " " } ;
          ? 4 ??? private String unit[] = { " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " } ;
          ? 5 ??? private String small[] = { " " , " " } ;
          ? 6 ??? private String strNumber,strUnit,strAll;
          ? 7
          ? 8 ??? private String onlyInt( int intInt)
          ? 9 ??? {
          10 ??????? String strInt;
          11 ??????? strInt = String.valueOf(intInt);
          12 ??????? strNumber = "" ;strUnit = "" ;strAll = "" ;
          13 ??????? int l = strInt.length ();
          14 ??????? int j,k,zeorCount;
          15 ??????? zeorCount = 0 ;
          16 ??????? for (k = 0 ;k < l;k ++ )
          17 ??????? {
          18 ??????????? String strTemp = strInt.substring(k,k + 1 );
          19 ??????????? int intTemp = Integer.parseInt(strTemp);
          20 ??????????? strNumber = number[intTemp];
          21 ??????????? j = l - 1 - k;
          22 ??????????? strUnit = unit[j];
          23 ??????????? if (intTemp == 0 )
          24 ??????????? {
          25 ??????????????? if (zeorCount == 0 )
          26 ??????????????? {
          27 ??????????????????? strUnit = strUnit.replace( ' ' , ' ' );
          28 ??????????????????? strUnit = strUnit.replace( ' ' , ' ' );
          29 ??????????????????? strUnit = strUnit.replace( ' ' , ' ' );
          30 ??????????????????? strUnit = strUnit.replace( ' ' , ' ' );???
          31 ??????????????? }

          32 ??????????????? else
          33 ??????????????? {
          34 ??????????????????? strUnit = strUnit.replaceAll( " " , "" );
          35 ??????????????????? strUnit = strUnit.replaceAll( " " , "" );
          36 ??????????????????? strUnit = strUnit.replaceAll( " " , "" );
          37 ??????????????????? strUnit = strUnit.replaceAll( " " , "" );???????
          38 ??????????????? }

          39 ??????????????? zeorCount ++ ;
          40 ??????????? }
          ???????
          41 ??????????? strAll += strNumber + strUnit;
          42 ??????? }

          43 ??????? return strAll;
          44 ???
          45 ??? }

          46 ???
          47 ??? private String onlySmall( int intSmall)
          48 ??? {
          49 ???????
          50 ??????? strNumber = "" ;strUnit = "" ;strAll = "" ;
          51 ??????? String strSmall,strTemp;
          52 ??????? strSmall = String.valueOf(intSmall);
          53 ??????? int i;
          54 ??????? if (intSmall >= 10 )
          55 ??????? {
          56 ??????????? for (i = 0 ;i < strSmall.length();i ++ )
          57 ??????????? {
          58 ??????????????? strTemp = String.valueOf(intSmall).substring(i,i + 1 );
          59 ??????????????? if (Integer.parseInt(strTemp) != 0 )
          60 ??????????????? {
          61 ??????????????????? strNumber = number[Integer.parseInt(strTemp)];
          62 ??????????????????? strUnit = small[i];???
          63 ??????????????????? strAll += strNumber + strUnit;
          64 ??????????????? }

          65 ??????????? }

          66 ??????? }

          67 ??????? else
          68 ??????? {
          69 ??????????? if (intSmall != 0 )
          70 ??????????? {
          71 ??????????????? strNumber = number[intSmall];
          72 ??????????????? strUnit = small[ 1 ];???
          73 ??????????????? strAll += strNumber + strUnit;
          74 ??????????? }

          75 ??????? }

          76
          77 ??????? return strAll;
          78 ??? }

          79 ???
          80 ??? public String getChineseMoney( double number)
          81 ??? {
          82 ??????? // 四舍五入
          83 ??????? number = (number * 100 + 0.5 ) / 100 ;
          84 ???????
          85 ??????? String strAll,strChineseInt,strChineseSmall,strZheng;;
          86 ??????? int intInt,intSmall;
          87 ??????? strChineseInt = "" ;strChineseSmall = "" ;strZheng = "" ;
          88 ???????
          89 ??????? // 整數(shù)部分
          90 ??????? intInt = ( int )( number * 100 / 100 );
          91 ??????? if (intInt != 0 )
          92 ??????? {
          93 ??????????? strChineseInt = onlyInt(intInt);
          94 ??????? }

          95 ??????? // 小數(shù)部分
          96 ??????? double temp = (number - intInt) * 100 * 100 / 100 ;
          97 ??????? // 對小數(shù)部分四舍五入
          98 ??????? intSmall = ( int )(temp * 100 + 0.5 ) / 100 ;
          99 ??????? if (intSmall != 0 )
          100 ??????? {
          101 ??????????? strChineseSmall = onlySmall(intSmall);
          102 ??????? }

          103 ??????? else
          104 ??????? {
          105 ??????????? strZheng = " " ;
          106 ??????? }

          107 ??????? strAll = strChineseInt + strChineseSmall + strZheng;
          108 ??????? return strAll;
          109 ??? }

          110 ??? public static void main(String args[]) throws IOException
          111 ??? {
          112 ??????? chineseMoney cm = new chineseMoney();
          113 ??????? double money;
          114 ??????? String strMoney,strChineseMoney;
          115 ??????? strMoney = "" ;
          116 ??????? // 讀取
          117 ??????? System.out.println( " 輸入貨幣(四舍五入): " );
          118 ??????? BufferedReader cin = new BufferedReader( new InputStreamReader( System.in));
          119 ??????? strMoney = cin.readLine();
          120 ??????? money = Double.parseDouble(strMoney);
          121 ??????? strChineseMoney = cm.getChineseMoney(money);
          122 ??????? System.out.println(strChineseMoney);
          123 ??? }

          124 }



          ?

          Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=641419

          Feedback

          # re: [收藏]JAVA中金額的中文大寫方式  回復(fù)  更多評論   

          2011-02-18 09:58 by oplife@163.com
          這個有個地方不太對,當(dāng)金額為800.00時,輸出的是:
          捌佰零圓整

          多了個零

          posts - 4, comments - 5, trackbacks - 0, articles - 60

          Copyright © ikingqu

          主站蜘蛛池模板: 会泽县| 大庆市| 永安市| 湘潭市| 原阳县| 额敏县| 新蔡县| 颍上县| 隆林| 铁岭县| 公安县| 镇巴县| 河东区| 巴彦县| 舒兰市| 勃利县| 灌南县| 徐汇区| 永康市| 大庆市| 新竹市| 伊宁县| 武定县| 电白县| 台州市| 阿图什市| 新源县| 贵港市| 巴马| 宁远县| 德化县| 渭源县| 启东市| 综艺| 含山县| 贵阳市| 高阳县| 孙吴县| 盐边县| 马关县| 吉林市|