在excel中有一列Value1,其中包含价格..例如$ 2789.98,$ 32321.45。我想获得这些值,并需要添加,例如:2789.98 + 32321.45。从Excel获取价格并需要使用POI计算值
public void testData() throws IOException {
Object[] calc = this.readExcelByColName("Calculations");
for (int i = 1; i < calc.length; i++) {
String string = (String) calc[i-1];
string = string.replaceAll("\\s", "");
String[] parts = string.split("(\\=)");
String[] parts1 = parts[0].split("(\\+)");
DataFormatter df = new DataFormatter();
if (parts.length == 2 && parts1.length > 1) {
String value1 = df.formatCellValue(this.sheet.getRow(i).getCell(2));
String value2 = df.formatCellValue(this.sheet.getRow(i).getCell(3));
String value3 = df.formatCellValue(this.sheet.getRow(i).getCell(4));
float result= (Float.parseFloat(value1.toString().substring(1).replaceAll(",", "")) + Float.parseFloat(value2.toString().substring(1).replaceAll(",", "")));
if (result == Float.parseFloat(value3.toString().substring(1).replaceAll(",", ""))) {
this.sheet.getRow(i).getCell(5).setCellValue("Pass");
} else {
this.sheet.getRow(i).getCell(5).setCellValue("Fail");
}
我可以能够得到的价格与$符号和删除使用substring,然后转换为整数,但在if(result == Integer.parseInt(value3)){condition,它不会将这两个值都添加到结果变量中。 –
运行int result = Integer.parseInt(value1.toString()。substring(1))+ Integer.parseInt(value2.toString()。substring(1))测试用例正在失败。 –
它没有返回任何东西,TC出错了。看起来转换有问题。我已检查转换为双倍,但没有工作。 –