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

📄 utilnumber.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
字号:
/* * $Id:$ * *  Copyright (c) 2003-2006 The Open For Business Project - www.ofbiz.org * *  Permission is hereby granted, free of charge, to any person obtaining a *  copy of this software and associated documentation files (the "Software"), *  to deal in the Software without restriction, including without limitation *  the rights to use, copy, modify, merge, publish, distribute, sublicense, *  and/or sell copies of the Software, and to permit persons to whom the *  Software is furnished to do so, subject to the following conditions: * *  The above copyright notice and this permission notice shall be included *  in all copies or substantial portions of the Software. * *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *  THE USE OR OTHER DEALINGS IN THE SOFTWARE. * *  @author Si Chen (sichen@opensourcestrategies.com) *  @author Leon Torres (leon@opensourcestrategies.com) */package org.ofbiz.base.util;import java.math.BigDecimal;import java.util.HashMap;import java.util.Locale;import com.ibm.icu.text.RuleBasedNumberFormat;public class UtilNumber {    public static String module = UtilNumber.class.getName();    // properties file name for arithmetic configuration    private static final String arithmeticPropertiesFile = "arithmetic.properties";    // default scale and rounding mode for BigDecimals    private static final int DEFAULT_BD_SCALE = 2;    private static final int DEFAULT_BD_ROUNDING_MODE = BigDecimal.ROUND_HALF_UP;     /**     * Method to get BigDecimal scale factor from a property     * @param   file     - Name of the property file     * @param   property - Name of the config property from arithmeticPropertiesFile (e.g., "invoice.decimals")     * @return  int - Scale factor to pass to BigDecimal's methods. Defaults to DEFAULT_BD_SCALE (2)     */    public static int getBigDecimalScale(String file, String property) {        if ((file == null) || (file.length() == 0)) return DEFAULT_BD_SCALE;        if ((property == null) || (property.length() == 0)) return DEFAULT_BD_SCALE;        int scale = -1;        String value = UtilProperties.getPropertyValue(file, property);        if (value != null) {            try {                scale = Integer.parseInt(value);            } catch (NumberFormatException e) {            }        }        if (scale == -1) {            Debug.logWarning("Could not set decimal precision from " + property + "=" + value + ". Using default scale of " + DEFAULT_BD_SCALE + ".", module);            scale = DEFAULT_BD_SCALE;        }        return scale;    }        /**     * As above, but use the default properties file     */    public static int getBigDecimalScale(String property) {        return getBigDecimalScale(arithmeticPropertiesFile, property);    }    /**     * Method to get BigDecimal rounding mode from a property     * @param   file     - Name of the property file     * @param   property - Name of the config property from arithmeticPropertiesFile (e.g., "invoice.rounding")     * @return  int - Rounding mode to pass to BigDecimal's methods. Defaults to DEFAULT_BD_ROUNDING_MODE (BigDecimal.ROUND_HALF_UP)     */    public static int getBigDecimalRoundingMode(String file, String property) {        if ((file == null) || (file.length() == 0)) return DEFAULT_BD_SCALE;        if ((property == null) || (property.length() == 0)) return DEFAULT_BD_ROUNDING_MODE;        String value = UtilProperties.getPropertyValue(file, property);        int mode = roundingModeFromString(value);        if (mode == -1) {            Debug.logWarning("Could not set decimal rounding mode from " + property + "=" + value + ". Using default mode of " + DEFAULT_BD_SCALE + ".", module);            return DEFAULT_BD_ROUNDING_MODE;        }        return mode;    }    /**     * As above, but use the default properties file     */    public static int getBigDecimalRoundingMode(String property) {        return getBigDecimalRoundingMode(arithmeticPropertiesFile, property);    }    /**     * Method to get the BigDecimal rounding mode int value from a string name.     * @param   value - The name of the mode (e.g., "ROUND_HALF_UP")     * @return  int - The int value of the mode (e.g, BigDecimal.ROUND_HALF_UP) or -1 if the input was bad.     */    public static int roundingModeFromString(String value) {        if (value == null) return -1;        value = value.trim();        if ("ROUND_HALF_UP".equals(value)) return BigDecimal.ROUND_HALF_UP;        else if ("ROUND_HALF_DOWN".equals(value)) return BigDecimal.ROUND_HALF_DOWN;        else if ("ROUND_HALF_EVEN".equals(value)) return BigDecimal.ROUND_HALF_EVEN;        else if ("ROUND_UP".equals(value)) return BigDecimal.ROUND_UP;        else if ("ROUND_DOWN".equals(value)) return BigDecimal.ROUND_DOWN;        else if ("ROUND_CEILING".equals(value)) return BigDecimal.ROUND_CEILING;        else if ("ROUND_FLOOR".equals(value)) return BigDecimal.ROUND_FLOOR;        else if ("ROUND_UNNECCESSARY".equals(value)) return BigDecimal.ROUND_UNNECESSARY;        return -1;    }    // ICU4J rule sets for the en_US locale. To add more rules, expand this string.    // For reference, see the RbnfSampleRuleSets.java file distributed with ICU4J    public static final String ruleSet_en_US =        /*         * These rules format a number in one of the two styles often used         * on checks.  %dollars-and-hundredths formats cents as hundredths of         * a dollar (23.40 comes out as "twenty-three and 40/100 dollars").         * %dollars-and-cents formats in dollars and cents (23.40 comes out as         * "twenty-three dollars and forty cents")         */        "%dollars-and-cents:\n"        + "    x.0: << [and >%%cents>];\n"        + "    0.x: >%%cents>;\n"        + "    0: zero dollars; one dollar; =%%main= dollars;\n"        + "%%main:\n"        + "    zero; one; two; three; four; five; six; seven; eight; nine;\n"        + "    ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen;\n"        + "        seventeen; eighteen; nineteen;\n"        + "    20: twenty[->>];\n"        + "    30: thirty[->>];\n"        + "    40: forty[->>];\n"        + "    50: fifty[->>];\n"        + "    60: sixty[->>];\n"        + "    70: seventy[->>];\n"        + "    80: eighty[->>];\n"        + "    90: ninety[->>];\n"        + "    100: << hundred[ >>];\n"        + "    1000: << thousand[ >>];\n"        + "    1,000,000: << million[ >>];\n"        + "    1,000,000,000: << billion[ >>];\n"        + "    1,000,000,000,000: << trillion[ >>];\n"        + "    1,000,000,000,000,000: =#,##0=;\n"        + "%%cents:\n"        + "    100: <%%main< cent[s];\n"        + "%dollars-and-hundredths:\n"        + "    x.0: <%%main< and >%%hundredths>/100;\n" // this used to end in 'dollars' but that should be added later        + "%%hundredths:\n"        + "    100: <00<;\n";    // hash map to store ICU4J rule sets keyed to Locale    public static HashMap rbnfRuleSets;    static {        rbnfRuleSets = new HashMap();        rbnfRuleSets.put(Locale.US, ruleSet_en_US);    }        /**     * Method to format an amount using a custom rule set.     * Current rule sets available:     *      * en_US     * %dollars-and-cents - 1,225.25 becomes "one thousand two hundred twenty five dollars and twenty five cents" (useful for checks)     * %dollars-and-hundreths - 1,225.25 becomes "one thousand two hundred twenty five and 25/00" (alternate for checks)     *     * @param   amount - the amount to format     * @param   rule - the name of the rule set to use (e.g., %dollars-and-hundredths)     * @param   locale - the Locale     * @return  formatted string or an empty string if there was an error     */    public static String formatRuleBasedAmount(double amount, String rule, Locale locale) {        String ruleSet = (String) rbnfRuleSets.get(locale);        if (ruleSet == null) {            Debug.logWarning("Cannot format rule based amount for locale " + locale.toString() + " because rule set for that locale does not exist", module);            return "";        }        RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(ruleSet, locale);        String result = "";        try {            result = formatter.format(amount, rule);        } catch (Exception e) {            Debug.logError(e, "Failed to format amount " + amount + " using rule " + rule, module);        }        return result;    }    /**     * Method to turn a number such as "0.9853" into a nicely formatted percent, "98.53%".     *     * @param number    The number object to format     * @param scale     How many places after the decimal to include     * @param roundingMode  The BigDecimal rounding mode to apply     * @return          The formatted string or "" if there were errors.     */    public static String toPercentString(Number number, int scale, int roundingMode) {        // convert to BigDecimal        if (!(number instanceof BigDecimal)) {            number = new BigDecimal(number.doubleValue());        }        // cast it so we can use BigDecimal methods        BigDecimal bd = (BigDecimal) number;        // multiply by 100 and set the scale        bd = bd.multiply(new BigDecimal(100.0)).setScale(scale, roundingMode);        return (bd.toString() + "%");    }}

⌨️ 快捷键说明

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