java.lang.NumberFormatException: For input string: "1,167.40"

The error is in the comma , in the middle of your input. A quick dirty solution would be removing it before parsing it as a double.

double val = Double.parseDouble(row[0].replaceAll(",", ""));

The better solution is using a NumberFormat to handle this:

//assumes your server already has English as locale
NumberFormat nf = NumberFormat.getInstance(); /
//...
double val = nf.parse(row).doubleValue();

You ought to use locale-specific formatting:

NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
myNumber = nf.parse(myString);

Some countries exchange the period and comma; ("1.167,40" in Locale.FRENCH, for example), so stripping the commas first is not safe.


It is because of character ',' (comma). The data here is represented for a specific locale. So you should use locale information to format the data in decimal first and then parse.

One such example is

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class JavaMain {
    public static void main(String[] args) {
        String numberString = "2.105,88";
        //using casting
        try {
            DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN);
            df.setParseBigDecimal(true);
            BigDecimal bd = (BigDecimal) df.parseObject(numberString);
            System.out.println(bd.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
        try {
            BigDecimal bd1 = new BigDecimal(nf.parse(numberString).toString());
            System.out.println(bd1.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}