Package com.jidesoft.converter
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 java.lang.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 Summary
Fields Modifier and Type Field Description static ConverterContext
CONTEXT_FRACTION_NUMBER
-
Constructor Summary
Constructors Constructor Description NumberConverter()
Creates a number converter with no NumberFormat.NumberConverter(java.text.NumberFormat format)
Creates the number converter with a specified NumberFormat.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.text.NumberFormat
getDefaultNumberFormat()
Gets the NumberFormat for Locale.US.protected java.text.NumberFormat
getNumberFormat()
Gets the NumberFormat.boolean
isAvoidNegativeZero()
Gets the flag indicating if negative zero should be avoided.static boolean
isGroupingUsed()
Gets flag if the grouping is used for the format.protected java.lang.Number
parseNumber(java.lang.String string)
Parse the string as number.void
setAvoidNegativeZero(boolean avoidNegativeZero)
Sets the flag indicating if negative zero should be avoided.void
setCurrency(java.util.Currency currency)
Set the currency of this converter.void
setFractionDigits(int minDigits, int maxDigits)
Set the fraction digits of this converter.static void
setGroupingUsed(boolean groupingUsed)
Sets if the grouping will be used for the NumberFormat.void
setIntegerDigits(int minDigits, int maxDigits)
Set the integer digits of this converter.void
setNumberFormat(java.text.NumberFormat numberFormat)
void
setRoundingMode(java.math.RoundingMode mode)
Set the rounding mode of this converter.boolean
supportToString(java.lang.Object object, ConverterContext context)
If it supports toString method.java.lang.String
toString(java.lang.Object object, ConverterContext context)
Converts from object to String based on current locale.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.jidesoft.converter.ObjectConverter
fromString, supportFromString
-
-
-
-
Field Detail
-
CONTEXT_FRACTION_NUMBER
public static final ConverterContext CONTEXT_FRACTION_NUMBER
-
-
Method Detail
-
toString
public java.lang.String toString(java.lang.Object object, ConverterContext context)
Description copied from interface:ObjectConverter
Converts from object to String based on current locale.- Specified by:
toString
in interfaceObjectConverter
- Parameters:
object
- object to be convertedcontext
- converter context to be used- Returns:
- the String
-
supportToString
public boolean supportToString(java.lang.Object object, ConverterContext context)
Description copied from interface:ObjectConverter
If it supports toString method.- Specified by:
supportToString
in interfaceObjectConverter
- Parameters:
object
- object to be convertedcontext
- converter context to be used- Returns:
- true if supports toString
-
setNumberFormat
public void setNumberFormat(java.text.NumberFormat numberFormat)
-
getNumberFormat
protected java.text.NumberFormat getNumberFormat()
Gets the NumberFormat. Of setNumberFormat is never called, it will return DecimalFormat.getInstance().- Returns:
- the NumberFormat.
-
getDefaultNumberFormat
protected java.text.NumberFormat getDefaultNumberFormat()
Gets the NumberFormat for Locale.US.- Returns:
- the NumberFormat for Locale.US.
-
parseNumber
protected java.lang.Number parseNumber(java.lang.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 digitsmaxDigits
- maximum fraction digits
-
setCurrency
public void setCurrency(java.util.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 digitsmaxDigits
- maximum integer digits
-
setRoundingMode
public void setRoundingMode(java.math.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(boolean)
-
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
-
-