Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting exponential value in java to a number format

Tags:

java

I am trying to read the values from excel sheet using java. When i type more than 10 letters in a cell in excel it is displaying in exponential form like "9.78313E+2". but this is not the real number what i given. Can any body help me out in this. How can i convert the above exponential form to original number using java language.

Thanks in advance

like image 486
user1853173 Avatar asked Nov 26 '12 11:11

user1853173


3 Answers

You can convert as follows, for example:

new BigDecimal("406770000244E+12").toBigInteger();
like image 175
user2996442 Avatar answered Nov 04 '22 16:11

user2996442


Double.parseDouble("9.78313E+2");

gives me

978.313

For more info see the doc.

Following your further queries below, if you've entered 4256411411 and Excel is presenting this as 4.26E+09, putting that value into parseDouble() will only give you 4260000000. If you want the original, perhaps you need to output the Excel file in a fuller format for your Java program, and/or query it using a Java/Excel API (e.g. POI)

like image 11
Brian Agnew Avatar answered Nov 04 '22 17:11

Brian Agnew


Sorry, but none of the answers above Double.parseDouble() and Double.valueOf()... solved my problem, and I continued to get the exponential 'E' value...

This link has a much better approach for the problem, and as I've written there - there is a very good solution:

I needed to convert some double to currency values, and fount that most to the solution are OK but not for me.

The DecimalFormat was eventually the way for me, so here is what I've done:

   public String foo(double value) //Got here 6.743240136E7 or something..
    {
        DecimalFormat formatter;

        if(value - (int)value > 0.0)
            formatter = new DecimalFormat("0.00"); //Here you can also deal with rounding if you wish..
        else
            formatter = new DecimalFormat("0");

        return formatter.format(value);
    }

As you can see, if the number is natural I get - say - 20000000 instead of 2E7 (etc) - without any decimal point.

and if it's decimal, I get only 2 decimal digits.

Hope this will help.

like image 5
JamesC Avatar answered Nov 04 '22 18:11

JamesC