shruti rawat
shruti rawat

Reputation: 251

Long.parseLong giving NumberFormatException

My issue is that

checksum = Long.parseLong("-986.9"); 

is giving NumberFormatException. Isn't this a parsible long?

Upvotes: 0

Views: 712

Answers (4)

enriquep92
enriquep92

Reputation: 1

Use a double, use the method Double.parseDouble(var)

Upvotes: 0

Bon
Bon

Reputation: 3103

long is an integer data type, -986.9 is not an integer. The below works for me.

long checksum = Long.parseLong("-986");
    System.out.println(checksum);
    double checksum2 = Double.parseDouble("-986.6");
    System.out.println(checksum2);

Upvotes: 0

fge
fge

Reputation: 121712

No it isn't. A long is a numeric integer type, and your number has a decimal point.

You want a double here (ie Double.parseDouble()).

Upvotes: 0

Gaël J
Gaël J

Reputation: 15070

A Long is not a decimal. Use Double and convert :

Double.parseDouble("-986.9").longValue(); 

Upvotes: 5

Related Questions