2016-03-02 20 views
0

我想知道單元格值的類型[double,integer,Number],如果它是十進制,則取決於此我想要將該十進制值四捨五入。如何實現這一目標?請幫助如何知道jxl中單元格的celltype?

代碼:

data1 = sh1.getCell(col, row).getContents(); 
ExcelCell exeCell=new ExcelCell(row, col, data1); 
for(ExcelCell x:list1){ 
      for(ExcelCell y:list2){ 

     if(x.rowNum==y.rowNum && x.colNum==y.colNum)// && x.cellValue!=null && y.cellValue!=null) 
        { 

      if(x.cellValue.matches(y.cellValue)){ 
         System.out.println("X:"+x.cellValue+ " Y:"+y.cellValue); 
        list3.add(new ExcelCell(x.rowNum, x.colNum, "MATCH")); 
        } 
        else { 
         System.out.println("X:"+x.cellValue+ " Y:"+y.cellValue); 
         list3.add(new ExcelCell(x.rowNum, x.colNum, "MIS-MATCH ("+x.cellValue+", "+y.cellValue+")")); 
        } 
       } 
      } 

回答

1
 data1 will always be string, One way to check type is: 

     //assuming data1 = 12.12 
     String data1 = "12.12"; 

       boolean isInt; 
       try{ 
        System.out.println(Integer.parseInt(data1)); 
        isInt = true; 
       }catch(NumberFormatException n1){ 
        isInt = false; 
       } 

       boolean isDouble; 
       try{ 
        System.out.println(Double.parseDouble(data1)); 
        isDouble = true; 
       }catch(NumberFormatException n2){ 
        isDouble = false; 
       } 

    Now if you want to check if its a double then use flag -- isDouble, it should be true, or if you want to check if its a number then OR of these two flag should be true: isDouble || isInt ==> true 

    //Checking if received data1 is double and valid number then 
    if(!isInt && isDouble) 
     { 
      double x = Double.parseDouble(data1); 
      System.out.println(Math.ceil(x)); 
      System.out.println(Math.floor(x)); 
     } 
    else if(isInt) 
     { 
      System.out.println("received int, do nothing"); 
     } 
+0

我想做的事是,如果DATA1具有雙重價值千萬一輪上漲的是其他有字符串值,然後繼續,因爲它是 –

+0

我添加了一個檢查,如果收到加倍然後做floor或ceil--你可以在這種情況下添加你的自定義邏輯。 –

相關問題