Isa Kuru
Isa Kuru

Reputation: 2089

java lang NumberFormatException

I am trying to convert string to double. But i am getting the number format exception. The code that gives the exception is like this:

double B = Double.parseDouble(s);

I take the s value from server to my application. And it is like this:

fd485b154a0427fa75c0428cdafac71f

Any solution?

Upvotes: 0

Views: 675

Answers (7)

Peter Lawrey
Peter Lawrey

Reputation: 533442

I would use

double doubleVal = new BigInteger("fd485b154a0427fa75c0428cdafac71f", 16).doubleValue();
System.out.println(doubleVal);

prints

3.366703756933705E38

If you use .longValue() you will be chopping off the start of the number due to an overflow.

Upvotes: 0

Matin Kh
Matin Kh

Reputation: 5178

Double.parseDouble(s) is doing just fine if s is number. Apparently you are receiving the number in hex base. Right?

It's simple as this:

double doubleAsLongReverse = new BigInteger(doubleAsString, 16).longValue();

Upvotes: 1

Vivek
Vivek

Reputation: 1326

Try this: (i am assuming the string is HEX coz of the 0-9 & a-f in it)

double doubleVal = new BigInteger("fd485b154a0427fa75c0428cdafac71f", 16).longValue();
long longVal = new BigInteger("fd485b154a0427fa75c0428cdafac71f", 16).longValue();
System.out.println(doubleVal);
System.out.println(longVal);

This gives:

8.4848548707027374E18
8484854870702737183

Upvotes: 1

Alexey Ogarkov
Alexey Ogarkov

Reputation: 2916

You can use ByteBuffer class http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html You can put bytes from string s.getBytes() and then call getDouble()

Hope it helps.

Upvotes: 0

martijno
martijno

Reputation: 1783

Use Hex in Apache's Commons Codec.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

1. Double.parseDouble() works only on Double Numbers , Not on non-Numbers.

2. fd485b154a0427fa75c0428cdafac71f is NO Number......

Upvotes: 2

J.K
J.K

Reputation: 2393

double B = Double.parseDouble(s);

Which is works only for numbers not for such fd485b154a0427fa75c0428cdafac71f values example String s = "123"; double B = Double.parseDouble(s); System.out.println(B);

Upvotes: 0

Related Questions