可能重复:
表示中的浮点错误?
因此,我正在运行此代码以从 aa 文件中读取浮点字符串值并将这些值存储到数组中。当程序从文件中读取“0.333”时,它会创建一个新的浮点值,但该值会转换为 0.33329999446868896!为什么是这样?我该如何解决这个问题?矩阵声明如下:
this.matrix = new double[this.getRow()][this.getCol()];
this.bMatrix = new double[this.getRow()];
try {
// make sure no blank lines at the end of the file.
for (int i = 0; (strLine = reader.readLine()) != null; i++) {
System.out.println("Line from file #" + i + " " + strLine);
String[] columns = strLine.trim().split("\\s+");
for (int j = 0; j < columns.length; j++) {
if (j < (columns.length - 1)) {
//A-matrix
this.matrix[i][j] = new Float(columns[j]).floatValue();
System.out.println("Element added to A-matrix: " + columns[j]);
} else {
// B-matrix
bMatrix[i] = new Float(columns[j]).floatValue();
System.out.println("Element added to B-matrix: " + columns[j]);
}
}
}
showMatrix();
} catch (IOException | NumberFormatException e) {
reader.close();
}
}
感谢您的任何帮助。