Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a String with an Exponent (Java)

I have a string similar to: 7.6E+7.

My question is simple: How do I turn this into its corresponding number: 76000000?

I have tried using substring to isolate the E+7 part, then parse the 7 part, then move the decimal places over 7. Is there an easier way to do this?

Thank you!

like image 742
Dylan Wheeler Avatar asked Mar 27 '26 19:03

Dylan Wheeler


2 Answers

long n = Double.valueOf("7.6E+7").longValue();
System.out.println(d);
// prints 76000000 to the output.
like image 88
sarveshseri Avatar answered Mar 30 '26 10:03

sarveshseri


I suggest using Double.parseDouble():

double val = Double.parseDouble(str);

where str is the input string.

like image 20
NPE Avatar answered Mar 30 '26 10:03

NPE



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!