金額轉換類

package com.util.NumberFormat;java

 

import java.math.BigDecimal;ide

import java.util.EnumMap;函數

 

public class NumberFormat {this

     

    // 1. 定義數字單位的枚舉類型orm

    public enum UnitOfMeasurement {對象

       /**ci

        * 利用構造函數傳參:萬,百萬,千萬,億,十億,百億,千億   rem

        * 因爲沒有對應的英文單詞表示get

        */string

       QIANYI(11),BAIIYI(10),YI(8),QIANWAN (7),BAIWAN (6),WAN (4),GEWEI(0);

 

       // 定義私有變量

       private int nCode ;

 

       // 構造函數,枚舉類型只能爲私有

       private UnitOfMeasurement( int _nCode) {

           this . nCode = _nCode;

       }

 

       @Override

       public String toString() {

           return String.valueOf ( this . nCode );

       }

    }

     

      /**

       * 建立數據計量單位的枚舉類型數據值

       * @return

       */

    private static  EnumMap<UnitOfMeasurement, String> creatEnumMap(String type) {

        // 1. 演示定義 EnumMap 對象, EnumMap 對象的構造函數須要參數傳入 , 默認是 key 的類的類型

        EnumMap<UnitOfMeasurement, String> currEnumMap = new EnumMap<UnitOfMeasurement, String>(UnitOfMeasurement. class );

        currEnumMap.put(UnitOfMeasurement. GEWEI , type );

        currEnumMap.put(UnitOfMeasurement. WAN , "萬" );

        currEnumMap.put(UnitOfMeasurement. BAIWAN , "百萬 " );

        currEnumMap.put(UnitOfMeasurement. QIANWAN , "千萬" );

        currEnumMap.put(UnitOfMeasurement. YI , "億" );

        currEnumMap.put(UnitOfMeasurement. BAIIYI , "百億 " );

        currEnumMap.put(UnitOfMeasurement. QIANYI , "千億" );

        return currEnumMap;

     }

     

     

      /**

       * 將BigDecimal 數據整理格式

       * 計算方式萬,百萬,千萬,億,

       * bigDecimal 最大值只支持到十億

       * @param number

       * @return

       */

      public static  String numberFormat(BigDecimal number,String type){

           String numberFormat="0";

           try{

                 if(type!=null){

                      numberFormat=numberFormat+type;

                 }

                 if(number!=null){

                       EnumMap<UnitOfMeasurement, String> currEnumMap =creatEnumMap(type);

                       for(UnitOfMeasurement aUnit : UnitOfMeasurement.values ()){

                             BigDecimal mu=(new BigDecimal(10)).pow(aUnit.nCode);//求冪值計算

                             if(number.compareTo(mu)>=0){

                                      String numberString=number.divide(mu, 2, BigDecimal.ROUND_HALF_UP).toString();

                                      System.out.println("numberString=="+numberString);

                                      String[] ret=numberString.split("\\.");

                                      if(ret[1].equals("00")){//去除小數位所有爲0狀況

                                            numberString=ret[0];

                                      }else{

                                            if(ret[1].endsWith("0")){//去除只有末尾爲0的狀況

                                                 numberString=numberString.substring(0, numberString.length()-1);

                                            }

                                      }

                                       numberFormat=numberString+currEnumMap.get(aUnit);

                                       break;

                             }

                        }

                 }

           }catch(Exception e){

                 e.printStackTrace();

           }

          

           return numberFormat.trim();

      }

     

     

     

     

/* * test方法*/

   public static void main(String[] args ) {

      //元

        String ss= numberFormat(new BigDecimal(210000),"元");

        System.out.println("ss=="+ss);

        //2萬 1.9

        String ss1= numberFormat(new BigDecimal(19999),"元");

        System.out.println("ss=="+ss1);

        //十萬21

        String ss2= numberFormat(new BigDecimal(210000),"元");

        System.out.println("ss=="+ss2);

        //百萬2.21

        String ss3= numberFormat(new BigDecimal(2210000),"元");

        System.out.println("ss=="+ss3);

        //千萬1.89

        String ss4= numberFormat(new BigDecimal(18900000),"元");

        System.out.println("ss=="+ss4);

        //億

        String ss5= numberFormat(new BigDecimal(189000000),"元");

        System.out.println("ss=="+ss5);

        //十億

        String ss6= numberFormat(new BigDecimal(1890000000),"元");

        System.out.println("ss=="+ss6);

       

       

   }

 

}

相關文章
相關標籤/搜索