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
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 -
Constructor Summary
ConstructorsConstructorDescriptionCreates a number converter with no NumberFormat.NumberConverter
(NumberFormat format) Creates the number converter with a specified NumberFormat. -
Method Summary
Modifier and TypeMethodDescriptionprotected NumberFormat
Gets the NumberFormat for Locale.US.protected NumberFormat
Gets the NumberFormat.boolean
Gets the flag indicating if negative zero should be avoided.static boolean
Gets flag if the grouping is used for the format.protected Number
parseNumber
(String string) Parse the string as number.void
setAvoidNegativeZero
(boolean avoidNegativeZero) Sets the flag indicating if negative zero should be avoided.void
setCurrency
(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
(NumberFormat numberFormat) void
setRoundingMode
(RoundingMode mode) Set the rounding mode of this converter.boolean
supportToString
(Object object, ConverterContext context) If it supports toString method.toString
(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 Details
-
CONTEXT_FRACTION_NUMBER
-
-
Constructor Details
-
NumberConverter
public NumberConverter()Creates a number converter with no NumberFormat. -
NumberConverter
Creates the number converter with a specified NumberFormat.- Parameters:
format
- the number format.
-
-
Method Details
-
toString
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
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
-
getNumberFormat
Gets the NumberFormat. Of setNumberFormat is never called, it will return DecimalFormat.getInstance().- Returns:
- the NumberFormat.
-
getDefaultNumberFormat
Gets the NumberFormat for Locale.US.- Returns:
- the NumberFormat for Locale.US.
-
parseNumber
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
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
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
-