开发者

How can I parse the string to Double

I want to parse 78 into double variable where 78 is stored as a String

I us开发者_Python百科ed below code to parse.

Double.parseDouble(78);

It display's Exception Error java.lang.NumberFormatException

Plz tell me How can I parse String 78 into double


Well what you passed was an integer and not a string.

To use it do this:

Double.parseDouble("78");


It shouldn't be Double.parseDouble(78) but Double.parseDouble("78") instead.

In that case it shouldn't give you any exception.


pass it as String

Double.parseDouble("78");


You need to put quotes around your parameter.

Double.parseDouble("78");


Well, I strongly suspect that the value of the string variable you're passing in actually isn't a valid number. I suggest you log the Unicode value of each character, so you can see exactly what's in there.

The code you've given (passing the string "78") should be absolutely fine... but you may have some extra, invisible characters in your real string. Logging the exact values should make that clear.


before you parse your string variable , use the method trim() in order to be sure that your string contain only numbers , , try this :

myStringToParse = myStringToParse.trim();
Double.parseDouble(myStringToParse);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜