0

I have value String "2248880.0" , i want to convert to Long in android with code :

Long.parseLong( "2248880.0"); 

But I have get Error

java.lang.NumberFormatException: Invalid long: "2248880.0"

so How to fix it ?

2

4 Answers 4

7

This is not Long value, so you need to parse it as Double or Float first. After that you can cast it to Long.

Or you can remove .000 from the string.

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

Comments

3

Use this -

long value = (long) Double.parseDouble("2248880.0"); 

This will cast your answer(which is double type) to long type.

Comments

3
java.lang.NumberFormatException: Invalid long: "2248880.0" 

NumberFormatException is an Exception that might be thrown when you try to convert a String into a number, where that number might be an int , a float , or any other Java numeric type.

At first remove .

Then Use.

long YourLongObj = Long.parseLong("Your_string");

Please read How can I convert String to Double without losing precision in Java

Comments

2

First Declare a Long Variable:

long l; 

Then Initialize it:

l= (long) Double.parseDouble("2248880.0"); 

This is because the value you have is in double format, so you have to first parse it into double and then from double to long.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.