Package org.supercsv.cellprocessor
Class FmtNumber
java.lang.Object
org.supercsv.cellprocessor.CellProcessorAdaptor
org.supercsv.cellprocessor.FmtNumber
- All Implemented Interfaces:
CellProcessor
,DoubleCellProcessor
,LongCellProcessor
public class FmtNumber
extends CellProcessorAdaptor
implements DoubleCellProcessor, LongCellProcessor
Converts a double into a formatted string using the
If you want to convert from a String to a decimal, use the
DecimalFormat
class and the default locale. This is
useful, when you need to show numbers with a specific number of digits.
Please be aware that the constructors that use DecimalFormat are not thread-safe, so it is generally better to use the constructors that accept a date format String.
In the format string, the following characters are defined as :
0 - means Digit # - means Digit, zero shows as absent (works only as zero padding on the right hand side of the number) . - means Decimal separator or monetary decimal separator - - means Minus sign , - means Grouping separator
If you want to convert from a String to a decimal, use the
ParseDouble
or ParseBigDecimal
processor.- Since:
- 1.50
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final String
the decimal format stringprivate final DecimalFormat
the decimal format object - not thread safeFields inherited from class org.supercsv.cellprocessor.CellProcessorAdaptor
next
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format String.FmtNumber
(String decimalFormat, StringCellProcessor next) Constructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format String, then calls the next processor in the chain.FmtNumber
(DecimalFormat formatter) Constructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format.FmtNumber
(DecimalFormat formatter, StringCellProcessor next) Constructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format, then calls the next processor in the chain. -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
checkPreconditions
(String dateFormat) Checks the preconditions for creating a new FmtNumber processor with a date format String.private static void
checkPreconditions
(DecimalFormat formatter) Checks the preconditions for creating a new FmtNumber processor with a DecimalFormat.execute
(Object value, CsvContext context) This method is invoked by the framework when the processor needs to process data or check constraints.Methods inherited from class org.supercsv.cellprocessor.CellProcessorAdaptor
toString, validateInputNotNull
-
Field Details
-
decimalFormat
the decimal format string -
formatter
the decimal format object - not thread safe
-
-
Constructor Details
-
FmtNumber
Constructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format String. This constructor is thread-safe.- Parameters:
decimalFormat
- the decimal format String (seeDecimalFormat
)- Throws:
NullPointerException
- if decimalFormat is null
-
FmtNumber
Constructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format String, then calls the next processor in the chain. This constructor is thread-safe.- Parameters:
decimalFormat
- the decimal format String (seeDecimalFormat
)next
- the next processor in the chain- Throws:
NullPointerException
- if decimalFormat or next is null
-
FmtNumber
Constructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format. This constructor is not thread-safe.- Parameters:
formatter
- the DecimalFormat- Throws:
NullPointerException
- if formatter is null
-
FmtNumber
Constructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format, then calls the next processor in the chain. This constructor is not thread-safe.- Parameters:
formatter
- the DecimalFormatnext
- the next processor in the chain- Throws:
NullPointerException
- if formatter or next is null
-
-
Method Details
-
checkPreconditions
Checks the preconditions for creating a new FmtNumber processor with a date format String.- Parameters:
dateFormat
- the date format String- Throws:
NullPointerException
- if dateFormat is null
-
checkPreconditions
Checks the preconditions for creating a new FmtNumber processor with a DecimalFormat.- Parameters:
formatter
- the DecimalFormat- Throws:
NullPointerException
- if formatter is null
-
execute
This method is invoked by the framework when the processor needs to process data or check constraints.- Specified by:
execute
in interfaceCellProcessor
- Parameters:
value
- the value to be processedcontext
- the CSV context- Returns:
- the result of cell processor execution
- Throws:
SuperCsvCellProcessorException
- if value is null or not a Number, or if an invalid decimalFormat String was supplied
-