Elad Benda2
Elad Benda2

Reputation: 15492

java.lang.NumberFormatException: For input string: "0.10"

Why do I get java.lang.NumberFormatException: For input string: "0.10"

Long.valueOf("0.10")

Upvotes: 1

Views: 2838

Answers (4)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

That String does not represent a long value but a floating point value, better represented as a double, float or BigDecimal.

Upvotes: 5

William F. Jameson
William F. Jameson

Reputation: 1843

The reason is that Long.valueOf(String) only does parsing, but refuses to do any coercion/rounding/narrowing conversion. This is clearly explained in the Javadoc:

Returns a Long object holding the value of the specified String. The argument is interpreted as representing a signed decimal long, exactly as if the argument were given to the parseLong(java.lang.String) method. The result is a Long object that represents the integer value specified by the string.

BTW in order to avoid misunderstanding of your future questions, make sure you provide your expectation, which will allow the reader to understand the angle of your question's "why".

Upvotes: 1

Reimeus
Reimeus

Reputation: 159844

0.10 is a floating point number rather than a long. Use one of the conversion methods from the Float or Double classes

double value = Double.valueOf("0.10");

Upvotes: 2

Aniket Thakur
Aniket Thakur

Reputation: 68975

floating point numbers are by default double in java and you cannot convert from double to long without a cast. You can try

Double.parseDouble("0.10");

If you want long you can do

double doubleValue = Double.parseDouble("0.10");
long longVal = (long)doubleValue ;
System.out.println(longVal);

Upvotes: 0

Related Questions