Parsing a string with an exponent (Java)

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

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

I tried using substringto isolate the part E+7, then parse the part 7, and then move the decimals over 7. Is there an easier way to do this?

Thank!

+3
source share
4 answers
long n = Double.valueOf("7.6E+7").longValue();
System.out.println(d);
// prints 76000000 to the output.
+4
source

I suggest using Double.parseDouble():

double val = Double.parseDouble(str);

where stris the input string.

+3
source

Double.parseDouble(), .

String e = "7.6E+7";
System.out.println(Double.parseDouble(e));

7.6E7. , E

NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
System.out.println(f.format(Double.parseDouble(e)));

76000000 . , 0,1 76000000.1

+2

If you are sure that the number can ultimately be written to an integer without loss of precision, than you could do:

int d = (int) Double.parseDouble("7.6E+7");
System.out.println(d);

What prints 76000000at the exit.

+1
source

All Articles