我想知道单元格值的类型[double,integer,Number],如果是十进制则依赖于此我想要舍入该十进制值。怎么实现呢?请帮忙
代码:
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+")"));
}
}
}
答案 0 :(得分: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");
}