1. 程式人生 > >人民幣數字大寫轉換 java實現 [附原始碼]

人民幣數字大寫轉換 java實現 [附原始碼]

動機

業務系統開發的報表中經常需要將人民幣數值轉換為大寫,所以江蘇南大先騰java研發框架中寫了一個通用的大寫轉換函式。由於報表的數值一般比較大所以在java開發中不建議使用float或者int型別,請用BigDecimal型別,為了更加方便呼叫函式的引數使用字串型別。

原始碼

    final static private String CNum[]={"零","壹","貳","叄","肆","伍","陸","柒","捌","玖"};
    final static private String CNum2[]={"〇","一","二","三","四","五","六","七","八"
,"九"}; final static private String CBit[]={"","拾","佰","仟"}; //拾佰仟萬拾佰仟億拾佰仟萬 /* * 將數值大寫 */ public static String capitalization(String szNum) { StringBuilder resstr = new StringBuilder(); String tmpstr = szNum.trim(); int sl = tmpstr.length(); int
sp=0; int dotpos = tmpstr.indexOf('.'); if(dotpos != -1){ while(sl>1 && tmpstr.charAt(sl-1) == '0') sl--; if(tmpstr.charAt(sl-1)=='.') sl--; if(sl != tmpstr.length()){ tmpstr = tmpstr.substring(0,sl); } }else
dotpos = sl; if(sl<1) return CNum[0]; if(tmpstr.charAt(0) == '-'){ resstr.append("負"); sp = 1; } String integerNum = tmpstr.substring(sp,dotpos-sp); String decimalNum =""; if(dotpos+1<sl) decimalNum = tmpstr.substring(dotpos+1); sl = integerNum.length(); sp=0; while(sp<sl && integerNum.charAt(sp)=='0') sp++; if(sp > 0) integerNum = integerNum.substring(sp); int inl = integerNum.length(); if(inl>0){ int h = (inl-1) % 4 ; int j = (inl-1) / 4 + 1; sp=0; boolean allzero = false; boolean preallzero = false; for(;j>0;j--){ int k=h; h = 3; boolean preiszero = allzero; allzero = true; for(;k>=0;k--,sp++){ if(integerNum.charAt(sp) == '0') preiszero = true; else{ allzero = false; if(preiszero) resstr.append("零"); preiszero = false; resstr.append(CNum[(byte) (integerNum.charAt(sp)) - 48]).append(CBit[k]); } }// end for k if(/*j!=0 &&*/ j % 2 == 0 ){ if(!allzero) resstr.append("萬"); } else { if(!allzero || !preallzero){ int repyi = j/2; for(int i=0; i<repyi; i++) resstr.append("億"); } } preallzero = allzero; }//end for j }else resstr.append("零"); int dnl = decimalNum.length(); if(dnl>0){ resstr.append("點"); for(int i=0; i<dnl; i++){ resstr.append(CNum[(byte)(decimalNum.charAt(i))-48]); } } return resstr.toString(); } /* * 僅僅是把 0~9 轉換為 "〇","一","二","三","四","五","六","七","八","九" */ public static String uppercaseCN(String szNum) { StringBuilder resstr = new StringBuilder(); String tmpstr = szNum.trim(); int sl = tmpstr.length(); int sp=0; if(sl<1) return CNum2[0]; for(;sp<sl;sp++) if(tmpstr.charAt(sp)>='0' && tmpstr.charAt(sp)<='9') resstr.append(CNum2[tmpstr.charAt(sp)-'0']); else resstr.append(tmpstr.charAt(sp)); return resstr.toString(); } public static String capitalization(String szNum,final boolean isSimple) { if (isSimple){ return uppercaseCN(szNum); } return capitalization(szNum); }

測試程式

public static String rmbDX(String f){
        return NumberBaseOpt.capitalization(
                        (f.indexOf('.')>=0? f.substring(0,f.indexOf('.')):f)) + "元"
        + NumberBaseOpt.capitalization(
                String.valueOf(NumberBaseOpt.getNumByte(f,-1)))+"角"
        + NumberBaseOpt.capitalization(
                String.valueOf(NumberBaseOpt.getNumByte(f,-2)))+"分";
    }

    public static void main(String[] args) {
        System.out.println(rmbDX("123.45"));
        System.out.println(rmbDX("3456700000000089123.45"));
    }

執行結果:

壹佰貳拾叄元肆角伍分
叄佰肆拾伍億億陸仟柒佰萬億零捌萬玖仟壹佰貳拾叄元肆角伍分