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
    Author:
    Kasper B. Graversen, James Bassett
    • Constructor Summary

      Constructors 
      Constructor Description
      FmtNumber​(java.lang.String decimalFormat)
      Constructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format String.
      FmtNumber​(java.lang.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​(java.text.DecimalFormat formatter)
      Constructs a new FmtNumber processor, which converts a double into a formatted string using the supplied decimal format.
      FmtNumber​(java.text.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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object execute​(java.lang.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 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • FmtNumber

        public FmtNumber​(java.lang.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:
        java.lang.NullPointerException - if decimalFormat is null
      • FmtNumber

        public FmtNumber​(java.lang.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:
        java.lang.NullPointerException - if decimalFormat or next is null
      • FmtNumber

        public FmtNumber​(java.text.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:
        java.lang.NullPointerException - if formatter is null
      • FmtNumber

        public FmtNumber​(java.text.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:
        java.lang.NullPointerException - if formatter or next is null
    • Method Detail

      • execute

        public java.lang.Object execute​(java.lang.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