简体   繁体   English

从字符串解析Double时出现NumberFormatException

[英]NumberFormatException when parsing a Double from string

Long story short here is my code that throws the number format exception:- 长话短说,这是我的引发数字格式异常的代码:

String percenta = "6.415‏";
holder.setPercent(Double.parseDouble(percenta));

I've tried a also Double.valueOf() but none of them work, still throws the same exception 我也尝试过Double.valueOf()但它们都不起作用,仍然抛出相同的异常

here is the exception message 这是异常消息

java.lang.NumberFormatException: For input string: "6.415‏"

what seems to be the problem here ? 这里似乎是什么问题?

EDIT 编辑

so the problem is in my encoding after all, how do i change that, I am using JSOUP to parse a page and it is supposed to be UTF-8, is Double.parseDouble not UTF-8 friendly ? 所以问题毕竟出在我的编码中,我该如何更改呢?我正在使用JSOUP来解析一个页面,它应该是UTF-8,Double.parseDouble不是UTF-8友好的吗?

Try using: 尝试使用:

NumberFormat format = NumberFormat.getInstance(Locale.US);
Number number = format.parse("6.415");
double d = number.doubleValue();

This may happen because of your internal Locale specification. 由于您的内部区域设置规范,可能会发生这种情况。

In order to verify it, check if it works: 为了验证它,请检查它是否有效:

String percenta = "6,415‏";
holder.setPercent(Double.parseDouble(percenta));

I'd guess it is because your computer is running a non-English locale. 我猜是因为您的计算机运行的是非英语语言环境。 Try Double.valueOf("6.415") for the English locale, or see Best way to parseDouble with comma as decimal separator? 尝试使用Double.valueOf(“ 6.415”)设置英语语言环境,还是请参阅使用逗号作为小数点分隔符的解析Double的最佳方法? for more info on parsing. 有关解析的更多信息。

The choice of variable name percenta suggests that you're using the locale sk-SK . 选择变量名percenta表示您正在使用语言环境sk-SK

The decimal separator for the locale is , and not . 语言环境的小数点分隔符是,而不是. .

6.415 is a valid value and should not throw a NumberFormatException . 6.415是有效值,并且不应抛出NumberFormatException
Check your encoding. 检查您的编码。 When I copy-pasted your code, got error while saving it. 当我复制粘贴您的代码时,在保存代码时出错。

在此处输入图片说明

The encoding for the 'percenta = "6.415‏"' is not supported for Double to parse. Double解析不支持'percenta =“ 6.415”'的编码。

Please check the below error when I tried to copy your code. 当我尝试复制您的代码时,请检查以下错误。

在此处输入图片说明

The problem is with the 6.415. 问题出在6.415。 It seems you are using keyboard that is not having compliant character encoding. 看来您使用的键盘没有兼容的字符编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM