Converting exponential value in java to a number format

前端 未结 12 1385
旧巷少年郎
旧巷少年郎 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:00

    This answer worked for me:

    Double bd = new Double(4445566622);
    System.out.println(bd.longValue());
    // Outputs -4445566622
    
    0 讨论(0)
  • 2020-12-06 06:02

    Try this definitely gona work
    double value = 2.06E //real 205809104.13
    BigDecimal.valueOf(value)
    work for me

    0 讨论(0)
  • 2020-12-06 06:05

    Have to convert the cell into number format before reading the cell value. Below is the code snippet that is used to get the actual value that is in exponential format:

    nextCell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
    Double doubleValue = nextCell.getNumericCellValue();
    BigDecimal bd = new BigDecimal(doubleValue.toString());
    long lonVal = bd.longValue();
    String phoneNumber = Long.toString(lonVal).trim();
    System.out.print("PhoneNumber " + phoneNumber);
    

    Blog has been wirtten to showcase the actual result.

    Regards, Ankur

    0 讨论(0)
  • 2020-12-06 06:08

    Before you read the value from excel sheet format your Column to number.

    This may be helps to you

    UPDATED

    HSSFCell cellE1 = row1.getCell((short) 4);
    cellE1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    Double e1Val = cellE1.getNumericCellValue();
    BigDecimal bd = new BigDecimal(e1Val.toString());
    long lonVal = bd.longValue();
    System.out.println(lonVal);
    
    0 讨论(0)
  • 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)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题