Converting exponential value in java to a number format

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

    You can use BigDecimal, if you want the exact value that you have in Excel Sheet: -

    BigDecimal bd = new BigDecimal("4256411411");
    System.out.println(bd.doubleValue());
    
    // If you are sure that's not a floating point number, then use
    System.out.println(bd.longValue());  
    

    Prints: -

    4.256411411E9  
    4256411411
    
    0 讨论(0)
  • 2020-12-06 06:12

    You can also use wrapper classes :

    Double bd=new Double(4445566622);
    System.out.println(bd.longValue());
    

    Outputs -4445566622

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

    i had same problem when i only needed String Data that is "1744949451" but it give "1.744949451E9" so this worked for me

              XSSFCell cell = cells.getCell(j);
              String value = cell.toString();
              if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
                //cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                value = cell.getRawValue();
              }
              Log.i("LOG", value + " " + cell.getCellType());
    
    0 讨论(0)
  • 2020-12-06 06:16

    Try the following pattern:

    Double dblValue = Double.parseDouble("1.99E+07");       
    String str = String.format("%.2f", dblValue);
    System.out.println(str);
    

    Output:

    run:
    19900000,00
    BUILD SUCCESSFUL (total time: 0 seconds)
    
    0 讨论(0)
  • 2020-12-06 06:17

    You can convert as follows, for example:

    new BigDecimal("406770000244E+12").toBigInteger();
    
    0 讨论(0)
  • 2020-12-06 06:19

    You can convert easily with the following methods:

    Double.valueOf("9.78313E+2").longValue() or

    BigDecimal bd = new BigDecimal("9.78313E+2");
    long val = bd.longValue();
    

    Assuming that the given number is in a String form.

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