Class FmtNumber

All Implemented Interfaces:
CellProcessor, DoubleCellProcessor, LongCellProcessor

public class FmtNumber extends CellProcessorAdaptor implements DoubleCellProcessor, LongCellProcessor
Converts a double into a formatted string using 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 Details

    • decimalFormat

      private final String decimalFormat
      the decimal format string
    • formatter

      private final DecimalFormat formatter
      the decimal format object - not thread safe
  • Constructor Details

    • FmtNumber

      public FmtNumber(String decimalFormat)
      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 (see DecimalFormat)
      Throws:
      NullPointerException - if decimalFormat is null
    • FmtNumber

      public 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. This constructor is thread-safe.
      Parameters:
      decimalFormat - the decimal format String (see DecimalFormat)
      next - the next processor in the chain
      Throws:
      NullPointerException - if decimalFormat or next is null
    • FmtNumber

      public FmtNumber(DecimalFormat formatter)
      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

      public 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. This constructor is not thread-safe.
      Parameters:
      formatter - the DecimalFormat
      next - the next processor in the chain
      Throws:
      NullPointerException - if formatter or next is null
  • Method Details

    • checkPreconditions

      private static void checkPreconditions(String dateFormat)
      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

      private static void checkPreconditions(DecimalFormat formatter)
      Checks the preconditions for creating a new FmtNumber processor with a DecimalFormat.
      Parameters:
      formatter - the DecimalFormat
      Throws:
      NullPointerException - if formatter is null
    • execute

      public Object execute(Object value, CsvContext context)
      This method is invoked by the framework when the processor needs to process data or check constraints.
      Specified by:
      execute in interface CellProcessor
      Parameters:
      value - the value to be processed
      context - 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