Class NumberConverter

java.lang.Object
com.jidesoft.converter.NumberConverter
All Implemented Interfaces:
ObjectConverter
Direct Known Subclasses:
ByteConverter, DoubleConverter, FloatConverter, IntegerConverter, LongConverter, NaturalNumberConverter, NumberFormatConverter, ShortConverter, YearNameConverter

public abstract class NumberConverter extends Object implements ObjectConverter
Converter which converts Number to String and converts it back. You can pass in a NumberFormat as UserObject of ConverterContext if you want to control the format of the number such as maximum decimal point etc.
  • Field Details

    • CONTEXT_FRACTION_NUMBER

      public static final ConverterContext CONTEXT_FRACTION_NUMBER
  • Constructor Details

    • NumberConverter

      public NumberConverter()
      Creates a number converter with no NumberFormat.
    • NumberConverter

      public NumberConverter(NumberFormat format)
      Creates the number converter with a specified NumberFormat.
      Parameters:
      format - the number format.
  • Method Details

    • toString

      public String toString(Object object, ConverterContext context)
      Description copied from interface: ObjectConverter
      Converts from object to String based on current locale.
      Specified by:
      toString in interface ObjectConverter
      Parameters:
      object - object to be converted
      context - converter context to be used
      Returns:
      the String
    • supportToString

      public boolean supportToString(Object object, ConverterContext context)
      Description copied from interface: ObjectConverter
      If it supports toString method.
      Specified by:
      supportToString in interface ObjectConverter
      Parameters:
      object - object to be converted
      context - converter context to be used
      Returns:
      true if supports toString
    • setNumberFormat

      public void setNumberFormat(NumberFormat numberFormat)
    • getNumberFormat

      protected NumberFormat getNumberFormat()
      Gets the NumberFormat. Of setNumberFormat is never called, it will return DecimalFormat.getInstance().
      Returns:
      the NumberFormat.
    • getDefaultNumberFormat

      protected NumberFormat getDefaultNumberFormat()
      Gets the NumberFormat for Locale.US.
      Returns:
      the NumberFormat for Locale.US.
    • parseNumber

      protected Number parseNumber(String string)
      Parse the string as number. It will try using getNumberFormat first then try getDefaultNumberFormat which is the US locale number format.
      Parameters:
      string - the string
      Returns:
      the Number. Null if the string is not a number.
    • isGroupingUsed

      public static boolean isGroupingUsed()
      Gets flag if the grouping is used for the format.
      Returns:
      if the grouping is used.
    • setGroupingUsed

      public static void setGroupingUsed(boolean groupingUsed)
      Sets if the grouping will be used for the NumberFormat. We will call NumberFormat#setGroupingUsed when we create the NumberFormat. Default is true. If you want to call this method, please make sure calling it before ObjectConverterManager is used.
      Parameters:
      groupingUsed - true or false.
    • setFractionDigits

      public void setFractionDigits(int minDigits, int maxDigits)
      Set the fraction digits of this converter.
      Parameters:
      minDigits - minimum fraction digits
      maxDigits - maximum fraction digits
    • setCurrency

      public void setCurrency(Currency currency)
      Set the currency of this converter.
      Parameters:
      currency - currency
    • setIntegerDigits

      public void setIntegerDigits(int minDigits, int maxDigits)
      Set the integer digits of this converter.
      Parameters:
      minDigits - minimum integer digits
      maxDigits - maximum integer digits
    • setRoundingMode

      public void setRoundingMode(RoundingMode mode)
      Set the rounding mode of this converter.
      Parameters:
      mode - rounding mode
    • isAvoidNegativeZero

      public boolean isAvoidNegativeZero()
      Gets the flag indicating if negative zero should be avoided.
      Returns:
      true if negative zero should be avoided. Otherwise false.
      Since:
      3.5.1
      See Also:
    • setAvoidNegativeZero

      public void setAvoidNegativeZero(boolean avoidNegativeZero)
      Sets the flag indicating if negative zero should be avoided.

      By default, the value is false to keep backward compatibility. If you don't like the string like "-0.00", please set this flag to true to remove the negative sign.

      Parameters:
      avoidNegativeZero - the flag
      Since:
      3.5.1