Class UtilNumber

java.lang.Object
org.apache.ofbiz.base.util.UtilNumber

public final class UtilNumber extends Object
  • Field Details

  • Method Details

    • getBigDecimalScale

      public static int getBigDecimalScale(String file, String property)
      Method to get BigDecimal scale factor from a property
      Parameters:
      file - - Name of the property file
      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)
    • getBigDecimalScale

      public static int getBigDecimalScale(String property)
      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 public static int getBigDecimalRoundingMode(String file, String property)
      Deprecated.
      Method to get BigDecimal rounding mode from a property
      Parameters:
      file - - Name of the property file
      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
    • getBigDecimalRoundingMode

      @Deprecated public static int getBigDecimalRoundingMode(String property)
      Deprecated.
      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:
      int - Rounding mode to pass to BigDecimal's methods. Defaults to RoundingMode.HALF_UP
    • getRoundingMode

      public static RoundingMode getRoundingMode(String file, String property)
      Method to get BigDecimal rounding mode from a property
      Parameters:
      file - - Name of the property file
      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)
    • getRoundingMode

      public static RoundingMode getRoundingMode(String property)
      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

      public static RoundingMode roundingModeFromString(String value)
      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

      public static String formatRuleBasedAmount(double amount, Locale locale)
      Method to format an amount using a custom rule set. Current rule sets available:
      Parameters:
      amount - - the amount to format
      locale - - 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 format
      ruleSet - - ruleSet to use
      rule - - 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 public static String toPercentString(Number number, int scale, int roundingMode)
      Method to turn a number such as "0.9853" into a nicely formatted percent, "98.53%".
      Parameters:
      number - The number object to format
      scale - How many places after the decimal to include
      roundingMode - The BigDecimal rounding mode to apply
      Returns:
      The formatted string or "" if there were errors.
    • toPercentString

      public 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%".
      Parameters:
      number - The number object to format
      scale - How many places after the decimal to include
      roundingMode - the RoundingMode rounding mode to apply
      Returns:
      The formatted string or "" if there were errors.
    • safeAdd

      public static BigDecimal safeAdd(BigDecimal left, BigDecimal right)
      A null-aware method for adding BigDecimal, but only for the right operand.
      Parameters:
      left - The number to add to
      right - 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: