3

I have a problem: a number is showing in scientific notation if it has 8 or more digits before the decimal point.
Is there a simple way to convert this number to decimal via a library or something?
I began creating a manual method to parse it out, but it seems overcomplicated.

Any help will be appreciated.

input example: 1.0225556677556E7

Edit: I also need to be able to identify a number that is in scientific notation

peggyLeggy
  • 45
  • 1
  • 7
  • possible duplicate of [Format double value in scientific notation](http://stackoverflow.com/questions/2944822/format-double-value-in-scientific-notation) – JAL Jun 04 '15 at 14:28
  • @JAL: That's the opposite. – T.J. Crowder Jun 04 '15 at 14:28
  • @T.J.Crowder Yes but the answers in there provide the relevant links to the documentation that will help the asker. – JAL Jun 04 '15 at 14:29
  • 1
    @JAL doesn't matter -- different question, different answer. SO is all about *not* having to read through all the docs. – Software Engineer Jun 04 '15 at 14:43

1 Answers1

5

You can use NumberFormat your accomplish your goal easily.

String scientificNotation = "1.0225556677556E7";
Double scientificDouble = Double.parseDouble(scientificNotation);
NumberFormat nf = new DecimalFormat("################################################.###########################################");
decimalString = nf.format(scientificDouble);

To answer you're other question about matching scientific notation strings- you can use a regex and String.matches(). This one isn't perfect although the probability of getting false positives should be very low:

if(myString.matches("-?[\\d.]+(?:E-?\\d+)?")){
    //do work
}
GregH
  • 5,125
  • 8
  • 55
  • 109