package org.supercsv.cellprocessor;

import java.text.DecimalFormat;

import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
import org.supercsv.cellprocessor.ift.LongCellProcessor;
import org.supercsv.cellprocessor.ift.StringCellProcessor;
import org.supercsv.exception.ClassCastInputCSVException;
import org.supercsv.exception.NullInputException;
import org.supercsv.exception.SuperCSVException;
import org.supercsv.util.CSVContext;

/**
 * Converts a double into a formatted string using the {@link DecimalFormat} class. This is useful, when you need to
 * show numbers with a specific number of digits.
 * <p>
 * In the format string, the following characters are defined as : <br>
 * 
 * <pre>
 * 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
 * </pre>
 * 
 * <br>
 * If you want to convert from a String to a decimal, use the {@link ParseDouble} or {@link ParseBigDecimal} processor.
 * 
 * @since 1.50
 * @author Kasper B. Graversen
 */
public class FmtNumber extends CellProcessorAdaptor implements DoubleCellProcessor, LongCellProcessor {

protected DecimalFormat formatter;

public FmtNumber(final String format) {
	super();
	this.formatter = new DecimalFormat(format);
}

public FmtNumber(final String format, final StringCellProcessor next) {
	super(next);
	this.formatter = new DecimalFormat(format);
}

/**
 * {@inheritDoc}
 */
@Override
public Object execute(final Object value, final CSVContext context) throws SuperCSVException {
	if( value == null ) { throw new NullInputException("Input cannot be null on line " + context.lineNumber
		+ " column " + context.columnNumber, context, this); }
	
	if( !(value instanceof Number) ) { throw new ClassCastInputCSVException("the value '" + value
		+ "' is not of type Number", context); }
	
	final String result = formatter.format(value);
	return next.execute(result, context);
}
}

@@ -1368,36 +1368,13 @@
 %7B%0D%0A%09
-super();%0D%0A%09this.formatter = 
+this(
 new 
@@ -1386,32 +1386,33 @@
 alFormat(format)
+)
 ;%0D%0A%7D%0D%0A%0D%0Apublic F
@@ -1481,65 +1481,274 @@
 %7B%0D%0A%09
-super(next);%0D%0A%09this.formatter = new DecimalFormat(format)
+this(new DecimalFormat(format),next);%0D%0A%7D%0D%0A%0D%0Apublic FmtNumber(final DecimalFormat formatter) %7B%0D%0A%09super();%0D%0A%09this.formatter = formatter;%0D%0A%7D%0D%0A%0D%0Apublic FmtNumber(final DecimalFormat formatter, final StringCellProcessor next) %7B%0D%0A%09super(next);%0D%0A%09this.formatter = formatter
 ;%0D%0A%7D

add of new constructors