Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA - Converstion String (With dot) to Long

I have problem about convertion from string to long value. Example :

String a = "4.0";
Long l= Long.parseLong(a);

When I execute, get exception:

Exception in thread "main" java.lang.NumberFormatException: For input string: "4.0"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at com.tests.Test2.main(Test2.java:69)

Any idea? Thanks!

like image 532
fahrizal89 Avatar asked Jun 28 '26 04:06

fahrizal89


2 Answers

4.0 isn't a long value It's a double, Try Double.parseDouble(a)

like image 112
SamHoque Avatar answered Jul 01 '26 07:07

SamHoque


You are attempting to covert a string with an unsupported character, ., into a Long. This is not supported by https://docs.oracle.com/javase/7/docs/api/java/lang/Long.html#parseLong(java.lang.String). Try Long l = Double.valueOf(a).longValue();: https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#valueOf(java.lang.String).

like image 21
Mark A. Fitzgerald Avatar answered Jul 01 '26 08:07

Mark A. Fitzgerald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!