package org.supercsv.cellprocessor;

import java.math.BigDecimal;

import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.cellprocessor.ift.StringCellProcessor;
import org.supercsv.exception.NullInputException;
import org.supercsv.exception.SuperCSVException;
import org.supercsv.util.CSVContext;

/**
 * Convert a string to a big decimal. It constructs BigDecimals from strings as recommended by the Javadoc (e.g.
 * <tt>new BigDecimal(0.1)</tt> yields unpredictable results, while <tt>new BidDecimal("0.1")</tt> yields
 * predictable results.
 * 
 * @since 1.30
 * @author Kasper B. Graversen
 */
public class ParseBigDecimal extends CellProcessorAdaptor implements StringCellProcessor {

public ParseBigDecimal() {
	super();
}

public ParseBigDecimal(final CellProcessor next) {
	super(next);
}

/**
 * {@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 + " at column " + context.columnNumber, context, this); }
	final BigDecimal result;
	if( value instanceof String ) {
		try {
			result = new BigDecimal((String) value);
		}
		catch(final Exception e) {
			throw new SuperCSVException("Parser error", context, e);
		}
	} else {
		throw new SuperCSVException("Can't convert \"" + value
			+ "\" to a BigDecimal. Input is not of type String, but of type " + value.getClass().getName(), context,
			this);
	}
	
	return next.execute(result, context);
}
}

@@ -61,16 +61,56 @@
 ecimal;%0D
+%0Aimport java.text.DecimalFormatSymbols;%0D
 %0A%0D%0Aimpor
@@ -767,539 +767,1254 @@
 %0A%0D%0Ap
-ublic ParseBigDecimal() %7B%0D%0A%09super();%0D%0A%7D%0D%0A%0D%0Apublic ParseBigDecimal(final CellProcessor next) %7B%0D%0A%09super(next);%0D%0A%7D%0D%0A%0D%0A/**%0D%0A * %7B@inheritDoc%7D%0D%0A */%0D%0A@Override%0D%0Apublic Object execute(final Object value, final CSVContext context) throws SuperCSVException %7B%0D%0A%09if( value == null ) %7B throw new NullInputException(%22Input cannot be null on line %22 + context.lineNumber + %22 at column %22 + context.columnNumber, context, this); %7D%0D%0A%09final BigDecimal result;%0D%0A%09if( value instanceof String ) %7B%0D%0A%09%09try %7B%0D%0A%09%09%09result = new BigDecimal((String) value);%0D%0A%09%09
+rivate DecimalFormatSymbols symbols;%0D%0A%0D%0Apublic ParseBigDecimal() %7B%0D%0A    this((DecimalFormatSymbols)null);%0D%0A%7D%0D%0A%0D%0Apublic ParseBigDecimal(DecimalFormatSymbols symbols) %7B%0D%0A    super();%0D%0A    this.symbols = symbols;%0D%0A%7D%0D%0A%0D%0Apublic ParseBigDecimal(final CellProcessor next) %7B%0D%0A    this(null,next);%0D%0A%7D%0D%0A%0D%0Apublic ParseBigDecimal(DecimalFormatSymbols symbols, final CellProcessor next) %7B%0D%0A    super(next);%0D%0A    this.symbols = symbols;%0D%0A%7D%0D%0A%0D%0A/**%0D%0A * %7B@inheritDoc%7D%0D%0A */%0D%0A@Override%0D%0Apublic Object execute(final Object value, final CSVContext context) throws SuperCSVException %7B%0D%0A%09if( value == null ) %7B throw new NullInputException(%22Input cannot be null on line %22 + context.lineNumber + %22 at column %22 + context.columnNumber, context, this); %7D%0D%0A%09final BigDecimal result;%0D%0A%09if( value instanceof String ) %7B%0D%0A%09%09try %7B%0D%0A            if (symbols == null) %7B%0D%0A                result = new BigDecimal((String) value);%0D%0A            %7D else %7B%0D%0A                if (symbols.getDecimalSeparator() != '.') %7B%0D%0A                    String s = (String) value;%0D%0A                    result = new BigDecimal(s.replace(symbols.getDecimalSeparator(), '.'));%0D%0A                %7D else %7B%0D%0A                    result = new BigDecimal((String) value);%0D%0A                %7D%0D%0A            %7D%0D%0A        
 %7D%0D%0A%09

taking into account possible DecimalFormatSymbols.