Converting exponential value in java to a number format

前端 未结 12 1470
旧巷少年郎
旧巷少年郎 2020-12-06 05:47

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

12条回答
  •  抹茶落季
    2020-12-06 06:12

    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.

提交回复
热议问题