java.lang.NumberFormatException:对于输入字符串:“1,167.40”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18758480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NumberFormatException: For input string: "1,167.40"
提问by Klausos Klausos
I'm reading data from CSV file. One of the fields contains the value 1,167.40
. The code piece for reading this field is the following:
我正在从 CSV 文件中读取数据。其中一个字段包含值1,167.40
。读取该字段的代码如下:
String csvFilename = "TEST_FILE.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
String[] row = null;
csvReader.readNext(); // to skip the headers
while((row = csvReader.readNext()) != null)
{
double val = Double.parseDouble(row[0]); // !!!
}
csvReader.close();
The line double val = Double.parseDouble(row[0])
results in the following error message:
该行double val = Double.parseDouble(row[0])
导致以下错误消息:
java.lang.NumberFormatException: For input string: "1,167.40"
java.lang.NumberFormatException:对于输入字符串:“1,167.40”
How to solve this issue?
如何解决这个问题?
P.S. For other values like 111.64
this error does not appear.
PS 对于其他类似111.64
这个错误的值不会出现。
采纳答案by Luiggi Mendoza
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
.
double val = Double.parseDouble(row[0].replaceAll(",", ""));
The bettersolution is using a NumberFormat
to handle this:
该更好的解决方案是使用NumberFormat
以处理这个问题:
//assumes your server already has English as locale
NumberFormat nf = NumberFormat.getInstance(); /
//...
double val = nf.parse(row).doubleValue();
回答by Gyanendra Dwivedi
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();
}
}
}
回答by Ean V
Try
尝试
NumberFormat.getInstance().parse(row[0])
instead of Double.parseDouble()
NumberFormat.getInstance().parse(row[0])
代替 Double.parseDouble()
回答by Bathsheba
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.
一些国家交换句号和逗号;(例如,Locale.FRENCH 中的“1.167,40”),因此首先去除逗号是不安全的。
回答by Mateusz Andrzejewski
Double.parseDouble()
does not like ,
in the strings you provide as parameter. Have you tried removing the ,
from the input? I assume 1,167.40 is the same as 1167.40.
Double.parseDouble()
不喜欢,
您作为参数提供的字符串。你有没有试过,
从输入中删除?我假设 1,167.40 与 1167.40 相同。
You can try:
你可以试试:
double val = Double.parseDouble(row[0].replace(',' , '');