熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java轉換大寫金額的代碼

2022-06-13   來源: Java核心技術 

  此代碼根據網上搜集到的部分代碼進行修改使得輸出准確完整

  package PrintOperate;

  import javatextNumberFormat;
    import javautilHashMap;

  public class SimpleMoneyFormat
    {

  public static final String EMPTY = ;
        public static final String ZERO = ;
        public static final String ONE = ;
        public static final String TWO = ;
        public static final String THREE = ;
        public static final String FOUR = ;
        public static final String FIVE = ;
        public static final String SIX = ;
        public static final String SEVEN = ;
        public static final String EIGHT = ;
        public static final String NINE = ;
        public static final String TEN = ;
        public static final String HUNDRED = ;
        public static final String THOUSAND = ;
        public static final String TEN_THOUSAND = ;
        public static final String HUNDRED_MILLION = ;
        public static final String YUAN = ;
        public static final String JIAO = ;
        public static final String FEN = ;
        public static final String DOT = ;
        private static SimpleMoneyFormat formatter = null;
        private HashMap chineseNumberMap = new HashMap();
        private HashMap chineseMoneyPattern = new HashMap();
        private NumberFormat numberFormat = NumberFormatgetInstance();

  private SimpleMoneyFormat()
        {
            numberFormatsetMaximumFractionDigits();
            numberFormatsetMinimumFractionDigits();
            numberFormatsetGroupingUsed(false);

  chineseNumberMapput( ZERO);
            chineseNumberMapput( ONE);
            chineseNumberMapput( TWO);
            chineseNumberMapput( THREE);
            chineseNumberMapput( FOUR);
            chineseNumberMapput( FIVE);
            chineseNumberMapput( SIX);
            chineseNumberMapput( SEVEN);
            chineseNumberMapput( EIGHT);
            chineseNumberMapput( NINE);
            chineseNumberMapput(DOT DOT);

  chineseMoneyPatternput( TEN);
            chineseMoneyPatternput( HUNDRED);
            chineseMoneyPatternput( THOUSAND);
            chineseMoneyPatternput( TEN_THOUSAND);
            chineseMoneyPatternput( TEN);
            chineseMoneyPatternput( HUNDRED);
            chineseMoneyPatternput( THOUSAND);
            chineseMoneyPatternput( HUNDRED_MILLION);
        }

  public static SimpleMoneyFormat getInstance()
        {
            if (formatter == null)
            {
                formatter = new SimpleMoneyFormat();
            }
            return formatter;
        }

  public String format(String moneyStr)
        {
            checkPrecision(moneyStr);
            String result;
            result = convertToChineseNumber(moneyStr);
            result = addUnitsToChineseMoneyString(result);
            return result;
        }

  public String format(double moneyDouble)
        {
            return format(numberFormatformat(moneyDouble));
        }

  public String format(int moneyInt)
        {
            return format(numberFormatformat(moneyInt));
        }

  public String format(long moneyLong)
        {
            return format(numberFormatformat(moneyLong));
        }

  public String format(Number moneyNum)
        {
            return format(numberFormatformat(moneyNum));
        }

  private String convertToChineseNumber(String moneyStr)
        {
            String result;
            StringBuffer cMoneyStringBuffer = new StringBuffer();
            for (int i = ; i < moneyStrlength(); i++)
            {
                cMoneyStringBufferappend(chineseNumberMapget(moneyStrsubstring(i i + )));
            }
            //拾佰仟萬億等都是漢字裡面才有的單位加上它們
            int indexOfDot = cMoneyStringBufferindexOf(DOT);
            int moneyPatternCursor = ;
            for (int i = indexOfDot ; i > ; i)
            {
                cMoneyStringBufferinsert(i chineseMoneyPatternget(EMPTY + moneyPatternCursor));
                moneyPatternCursor = moneyPatternCursor == ? : moneyPatternCursor + ;
            }

  String fractionPart = cMoneyStringBuffersubstring(cMoneyStringBufferindexOf());
            cMoneyStringBufferdelete(cMoneyStringBufferindexOf() cMoneyStringBufferlength());
            while (cMoneyStringBufferindexOf(零拾) != )
            {
                cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零拾) cMoneyStringBufferindexOf(零拾) + ZERO);
            }
            while (cMoneyStringBufferindexOf(零佰) != )
            {
                cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零佰) cMoneyStringBufferindexOf(零佰) + ZERO);
            }
            while (cMoneyStringBufferindexOf(零仟) != )
            {
                cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零仟) cMoneyStringBufferindexOf(零仟) + ZERO);
            }
            while (cMoneyStringBufferindexOf(零萬) != )
            {
                cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零萬) cMoneyStringBufferindexOf(零萬) + TEN_THOUSAND);
            }
            while (cMoneyStringBufferindexOf(零億) != )
            {
                cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零億) cMoneyStringBufferindexOf(零億) + HUNDRED_MILLION);
            }
            while (cMoneyStringBufferindexOf(零零) != )
            {
                cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零零) cMoneyStringBufferindexOf(零零) + ZERO);
            }
            if (cMoneyStringBufferlastIndexOf(ZERO) == cMoneyStringBufferlength() )
            {
                cMoneyStringBufferdelete(cMoneyStringBufferlength() cMoneyStringBufferlength());
            }
            cMoneyStringBufferappend(fractionPart);

  result = cMoneyStringBuffertoString();
            return result;
        }

  private String addUnitsToChineseMoneyString(String moneyStr)
        {
            String result;
            StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);
            int indexOfDot = cMoneyStringBufferindexOf(DOT);
            cMoneyStringBufferreplace(indexOfDot indexOfDot + YUAN);
            cMoneyStringBufferinsert(cMoneyStringBufferlength() JIAO);
            cMoneyStringBufferinsert(cMoneyStringBufferlength() FEN);
            if (cMoneyStringBufferindexOf(零角零分) != )//沒有零頭加整
            {
                cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零角零分) cMoneyStringBufferlength() );
            }
            else if (cMoneyStringBufferindexOf(零分) != )//沒有零分不輸出零分也不加整
            {
                cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零分) cMoneyStringBufferlength() );
            }
            else if (cMoneyStringBufferindexOf(零角) != )
            {
                if(cMoneyStringBufferindexOf(零角)==(cMoneyStringBufferlength()))
                {
                    cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零角) cMoneyStringBufferindexOf(零角) + ZERO);
                }
                else
                {
                    cMoneyStringBufferdelete(cMoneyStringBufferindexOf(零角) cMoneyStringBufferindexOf(零角) + );
                }
            }
            result = cMoneyStringBuffertoString();
            return result;
        }

  private void checkPrecision(String moneyStr)
        {
            int fractionDigits = moneyStrlength() moneyStrindexOf(DOT) ;
            if (fractionDigits > )
            {
                throw new RuntimeException(金額 + moneyStr + 的小數位多於兩位); //精度不能比分低
            }
        }

  public static void main(String args[])
        {
            Systemoutprintln(getInstance()format(new Double()));
        }
    }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26854.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.