Package org.apache.ofbiz.base.util
Class UtilNumber
java.lang.Object
org.apache.ofbiz.base.util.UtilNumber
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
formatRuleBasedAmount
(double amount, String ruleSet, String rule, Locale locale) Method to format an amount using a custom rule set.static String
formatRuleBasedAmount
(double amount, Locale locale) Method to format an amount using a custom rule set.static BigDecimal
getBigDecimal
(Map<String, ?> context, String field, BigDecimal defaultValue) Resolve a BigDecimal for a given field present on context map and return default value if not presentstatic int
getBigDecimalRoundingMode
(String property) Deprecated.static int
getBigDecimalRoundingMode
(String file, String property) Deprecated.UsegetRoundingMode(String, String)
insteadstatic int
getBigDecimalScale
(String property) Method to get BigDecimal scale factor from a property.static int
getBigDecimalScale
(String file, String property) Method to get BigDecimal scale factor from a propertystatic RoundingMode
getRoundingMode
(String property) Method to get BigDecimal rounding mode from a property.static RoundingMode
getRoundingMode
(String file, String property) Method to get BigDecimal rounding mode from a propertystatic RoundingMode
roundingModeFromString
(String value) Method to get the RoundingMode rounding mode int value from a string name.static BigDecimal
safeAdd
(BigDecimal left, BigDecimal right) A null-aware method for adding BigDecimal, but only for the right operand.static String
toPercentString
(Number number, int scale, int roundingMode) Deprecated.static String
toPercentString
(Number number, int scale, RoundingMode roundingMode) Method to turn a number such as "0.9853" into a nicely formatted percent, "98.53%".
-
Field Details
-
RULE_SET_EN_IN
- See Also:
-
-
Method Details
-
getBigDecimalScale
Method to get BigDecimal scale factor from a property- Parameters:
file
- - Name of the property fileproperty
- - Name of the config property from ARITH_PROP_FILE (e.g., "invoice.decimals")- Returns:
- int - Scale factor to pass to BigDecimal's methods. Defaults to DEFAULT_BD_SCALE (2)
-
getBigDecimalScale
Method to get BigDecimal scale factor from a property. Use the default ARITH_PROP_FILE properties file- Parameters:
property
- - Name of the config property from ARITH_PROP_FILE (e.g., "invoice.decimals")- Returns:
- int - Scale factor to pass to BigDecimal's methods. Defaults to DEFAULT_BD_SCALE (2)
-
getBigDecimalRoundingMode
Deprecated.UsegetRoundingMode(String, String)
insteadMethod to get BigDecimal rounding mode from a property- Parameters:
file
- - Name of the property fileproperty
- - Name of the config property from ARITH_PROP_FILE (e.g., "invoice.rounding")- Returns:
- int - Rounding mode to pass to BigDecimal's methods. Defaults to RoundingMode.HALF_UP
-
getBigDecimalRoundingMode
Deprecated.UsegetRoundingMode(String)
insteadMethod to get BigDecimal rounding mode from a property. Use the default ARITH_PROP_FILE properties file- Parameters:
property
- - Name of the config property from ARITH_PROP_FILE (e.g., "invoice.rounding")- Returns:
- int - Rounding mode to pass to BigDecimal's methods. Defaults to RoundingMode.HALF_UP
-
getRoundingMode
Method to get BigDecimal rounding mode from a property- Parameters:
file
- - Name of the property fileproperty
- - Name of the config property from ARITH_PROP_FILE (e.g., "invoice.rounding")- Returns:
- RoundingMode - Rounding mode to pass to BigDecimal's methods. Defaults to DEFAULT_BD_ROUNDING_MODE (RoundingMode.HALF_UP)
-
getRoundingMode
Method to get BigDecimal rounding mode from a property. Use the default ARITH_PROP_FILE properties file- Parameters:
property
- - Name of the config property from ARITH_PROP_FILE (e.g., "invoice.rounding")- Returns:
- RoundingMode - Rounding mode to pass to BigDecimal's methods. Defaults to DEFAULT_BD_ROUNDING_MODE (RoundingMode.HALF_UP)
-
roundingModeFromString
Method to get the RoundingMode rounding mode int value from a string name.- Parameters:
value
- - The name of the mode (e.g., "ROUND_HALF_UP")- Returns:
- RoundingMode - The rounding mode value of the mode (e.g, RoundingMode.HALF_UP) or null if the input was bad.
-
formatRuleBasedAmount
Method to format an amount using a custom rule set. Current rule sets available:- Parameters:
amount
- - the amount to formatlocale
- - the Locale- Returns:
- formatted string or an empty string if there was an error
-
formatRuleBasedAmount
public static String formatRuleBasedAmount(double amount, String ruleSet, String rule, Locale locale) 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)- Parameters:
amount
- - the amount to formatruleSet
- - ruleSet to userule
- - the name of the rule set to use (e.g., %dollars-and-hundredths)locale
- - the Locale- Returns:
- formatted string or an empty string if there was an error
-
toPercentString
Deprecated.Method to turn a number such as "0.9853" into a nicely formatted percent, "98.53%".- Parameters:
number
- The number object to formatscale
- How many places after the decimal to includeroundingMode
- The BigDecimal rounding mode to apply- Returns:
- The formatted string or "" if there were errors.
-
toPercentString
Method to turn a number such as "0.9853" into a nicely formatted percent, "98.53%".- Parameters:
number
- The number object to formatscale
- How many places after the decimal to includeroundingMode
- the RoundingMode rounding mode to apply- Returns:
- The formatted string or "" if there were errors.
-
safeAdd
A null-aware method for adding BigDecimal, but only for the right operand.- Parameters:
left
- The number to add toright
- The number being added; if null, then nothing will be added- Returns:
- The result of the addition, or left if right is null.
-
getBigDecimal
public static BigDecimal getBigDecimal(Map<String, ?> context, String field, BigDecimal defaultValue) Resolve a BigDecimal for a given field present on context map and return default value if not present- Parameters:
context
-field
-defaultValue
-- Returns:
-
getRoundingMode(String)
instead