私のJava開発学習の旅-->Java金額の大文字と小文字の変換

5934 ワード

/**
  * @ClassName: NumberConvert
  * @Description: TODO
 * @date 2013-3-07   16:08:10
  * @version V1.0
  */
public class MoneyUtil {
    /**      */
    private static final String[] NUMBERS = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    /**         */
    private static final String[] IUNIT = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
            " " };
    /**         */
    private static final String[] DUNIT = { " ", " ", " " };

    /**
     *       。
     */
    public static String toChinese(String str) {
        str = str.replaceAll(",", "");//   ","
        String integerStr;//       
        String decimalStr;//       

        //    :           
        if (str.indexOf(".") > 0) {
            integerStr = str.substring(0, str.indexOf("."));
            decimalStr = str.substring(str.indexOf(".") + 1);
        } else if (str.indexOf(".") == 0) {
            integerStr = "";
            decimalStr = str.substring(1);
        } else {
            integerStr = str;
            decimalStr = "";
        }
        // integerStr   0,    decimalStr  0(      )
        if (!integerStr.equals("")) {
            integerStr = Long.toString(Long.parseLong(integerStr));
            if (integerStr.equals("0")) {
                integerStr = "";
            }
        }
        // overflow      ,    
        if (integerStr.length() > IUNIT.length) {
            System.out.println(str + ":      ");
            return str;
        }

        int[] integers = toArray(integerStr);//       
        boolean isMust5 = isMust5(integerStr);//      
        int[] decimals = toArray(decimalStr);//       
        return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals);
    }

    /**
     *               ,      
     */
    private static int[] toArray(String number) {
        int[] array = new int[number.length()];
        for (int i = 0; i < number.length(); i++) {
            array[i] = Integer.parseInt(number.substring(i, i + 1));
        }
        return array;
    }

    /**
     *            。
     */
    private static String getChineseInteger(int[] integers, boolean isMust5) {
        StringBuffer chineseInteger = new StringBuffer("");
        int length = integers.length;
        for (int i = 0; i < length; i++) {
            // 0       :1234( )5678( )9012( )3456( )
            //     :10(  、   、    、   )
            String key = "";
            if (integers[i] == 0) {
                if ((length - i) == 13)//  ( )(  )
                    key = IUNIT[4];
                else if ((length - i) == 9)//  (  )
                    key = IUNIT[8];
                else if ((length - i) == 5 && isMust5)//  (   )
                    key = IUNIT[4];
                else if ((length - i) == 1)//  (  )
                    key = IUNIT[0];
                // 0  0   ,       
                if ((length - i) > 1 && integers[i + 1] != 0)
                    key += NUMBERS[0];
            }
            chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
        }
        return chineseInteger.toString();
    }

    /**
     *            。
     */
    private static String getChineseDecimal(int[] decimals) {
        StringBuffer chineseDecimal = new StringBuffer("");
        for (int i = 0; i < decimals.length; i++) {
            //   3      
            if (i == 3)
                break;
            chineseDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i]));
        }
        return chineseDecimal.toString();
    }

    /**
     *    5      " "    。
     */
    private static boolean isMust5(String integerStr) {
        int length = integerStr.length();
        if (length > 4) {
            String subInteger = "";
            if (length > 8) {
                //       , 5  8    
                subInteger = integerStr.substring(length - 8, length - 4);
            } else {
                subInteger = integerStr.substring(0, length - 4);
            }
            return Integer.parseInt(subInteger) > 0;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        String number = "1.23";
        System.out.println(number + " " + MoneyUtil.toChinese(number));
        number = "1234567890123456.123";
        System.out.println(number + " " + MoneyUtil.toChinese(number));
        number = "0.0798";
        System.out.println(number + " " + MoneyUtil.toChinese(number));
        number = "10,001,000.09";
        System.out.println(number + " " + MoneyUtil.toChinese(number));
        number = "01.107700";
        System.out.println(number + " " + MoneyUtil.toChinese(number));
    }

}
 
     :1.23       
1234567890123456.123                                      
0.0798     
10,001,000.09          
01.107700       
1000000001.107700            

==================================================================================================
作者:欧陽鵬は転載を歓迎して、人と分かち合うのは進歩の源泉です!
転載は原文の住所を保留してください:http://blog.csdn.net/ouyang_peng
==================================================================================================