⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 amtinwords_zh_cn.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 JAVA
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2005 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.util;

import java.text.DecimalFormat;


/**
 *	Amount in Words for zh_CN
 *
 *  @author Jorg Janke - http://www.rgagnon.com/javadetails/java-0426.html
 *  @version $Id: AmtInWords_zh_CN.java,v 1.2 2005/04/19 04:43:31 jjanke Exp $
 */
public class AmtInWords_zh_CN implements AmtInWords {
    private final static DecimalFormat nf = new DecimalFormat("#####0.00");

    /**************************************************************************
     * 	Get Amount in Words
     * 	@param amount numeric amount (352.80)
     * 	@return amount in words (three*five*two 80/100)
     */
    public String getAmtInWords(String amount) throws Exception {
        if (amount == null)
            return amount;
        int pos = amount.lastIndexOf('.');
        int pos2 = amount.lastIndexOf(',');
        if (pos2 > pos) {
            while (amount.indexOf('?') > 0) {
                amount = amount.substring(0, amount.indexOf('?')) +
                         amount.substring(amount.indexOf('?') + 1);
            } while (amount.indexOf('.') > 0) {
                amount = amount.substring(0, amount.indexOf('.')) +
                         amount.substring(amount.indexOf('.') + 1);
            }
            if (pos < 0) {
                amount = amount.replaceAll(",", ".");
            }
        } else {
            amount = amount.trim().replaceAll(",", "");
        }
        //
        String returnVal = "";
        String strUnit = "\u5146\u4e07\u4edf\u4f70\u62fe\u4ebf\u4edf\u4f70\u62fe\u4e07\u4edf\u4f70\u62fe\u5143\u89d2\u5206";
        String convertAmt = nf.format(Double.parseDouble(amount.replaceAll(" ",
                "")));
        convertAmt = convertAmt.substring(0, convertAmt.indexOf(".")) +
                     convertAmt.substring(convertAmt.indexOf(".") + 1);
        strUnit = strUnit.substring(strUnit.length() - convertAmt.length());
        for (int i = 0; i < convertAmt.length(); i++) {
            returnVal +=
                    "\u96f6\u58f9\u8d30\u53c1\u8086\u4f0d\u9646\u67d2\u634c\u7396".substring(Integer.parseInt(convertAmt.
                    substring(i, i + 1)),
                                           Integer.parseInt(convertAmt.substring(i, i + 1)) + 1) +
                    strUnit.substring(i, i + 1);

        }

        while (returnVal.indexOf("\u96f6\u5143") > 0 ||
               returnVal.indexOf("\u96f6\u62fe") > 0 ||
               returnVal.indexOf("\u96f6\u4f70") > 0 ||
               returnVal.indexOf("\u96f6\u4edf") > 0 ||
               returnVal.indexOf("\u96f6\u4e07") > 0 ||
               returnVal.indexOf("\u96f6\u4ebf") > 0 ||
               returnVal.indexOf("\u96f6\u96f6") > 0 ||
               returnVal.indexOf("\u4ebf\u4e07") > 0) {
            returnVal = replaceStr(returnVal, "\u96f6\u5143", "\u5143");
            returnVal = replaceStr(returnVal, "\u96f6\u62fe", "\u96f6");
            returnVal = replaceStr(returnVal, "\u96f6\u4f70", "\u96f6");
            returnVal = replaceStr(returnVal, "\u96f6\u4edf", "\u96f6");
            returnVal = replaceStr(returnVal, "\u96f6\u4e07", "\u4e07");
            returnVal = replaceStr(returnVal, "\u96f6\u4ebf", "\u4ebf");
            returnVal = replaceStr(returnVal, "\u96f6\u96f6", "\u96f6");
            returnVal = replaceStr(returnVal, "\u4ebf\u4e07", "\u4ebf");
        }
        returnVal = replaceStr(returnVal, "\u96f6\u89d2\u96f6\u5206", "\u6574");
        returnVal = replaceStr(returnVal, "\u96f6\u5206", "");

        return returnVal;
    } //	getAmtInWords

    /**
     * 	Test Print
     *	@param amt amount
     */
    private void print(String amt) {
        try {
            System.out.println(amt + " = " + getAmtInWords(amt));
        } catch (Exception e) {
            e.printStackTrace();
        }
    } //	print

    private static String replaceStr(String returnVal, String replaceStr,
                                     String replaceTo) {
        while (returnVal.indexOf(replaceStr) > 0) {
            returnVal = returnVal.substring(0, returnVal.indexOf(replaceStr)) +
                        replaceTo +
                        returnVal.substring(returnVal.indexOf(replaceStr) +
                                            replaceStr.length());
        }
        return returnVal;
    }

    /**
     * 	Test
     *	@param args ignored
     */
    public static void main(String[] args) {
        AmtInWords_zh_CN aiw = new AmtInWords_zh_CN();
        aiw.print("239093202.0");
        aiw.print("302830.0");
        aiw.print("2398238.52");
        aiw.print("10003872.0");
        aiw.print("987037803.0");
        aiw.print("503201001.0");
        aiw.print("5982010.5");
        aiw.print("5301300.0");
        aiw.print("52323400.0");
        aiw.print("501000132000.0");
        aiw.print("5000123320.0");
        aiw.print("5001000000.0");
        aiw.print("5010000000.0");
        aiw.print("5100000000.0");
        //aiw.print("1,234 578.90");

    } //	main

} //	AmtInWords_zh_CN

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -