0

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?

2
  • 2
    fd485b154a0427fa75c0428cdafac71f is the number in which base? Commented Aug 8, 2012 at 7:42
  • @JigarJoshi I guess hex... As there is no dot or comma, I guess it is an integer number (not int though, much too high) Commented Aug 8, 2012 at 7:45

7 Answers 7

2

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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();

Comments

0
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);

Comments

0

Use Hex in Apache's Commons Codec.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.