比较java中的十进制值
我试图从逗号分隔的CVS文件中获取某些值。到目前为止,这是我的代码:比较java中的十进制值,java,scientific-notation,Java,Scientific Notation,我试图从逗号分隔的CVS文件中获取某些值。到目前为止,这是我的代码: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.math.BigDecimal; class A { public static void main(String args[]){ BufferedReader br = null; t
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
class A {
public static void main(String args[]){
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("sample.csv"));
double start = 20659200000.000000;
DecimalFormat df = new DecimalFormat("#.######");
df.format(start);
int i = 0;
while ((sCurrentLine = br.readLine()) != null) {
String[] tmp = sCurrentLine.split(",");
//System.out.println(tmp[0]);
BigDecimal bg1, bg2;
bg1 = new BigDecimal(Double.parseDouble(new
BigDecimal(tmp[0]).toPlainString()));
bg2 = new BigDecimal(start);
if(bg1.equals(bg2))
{
//System.out.println("Inside if");
//System.out.println(tmp[0]);
System.out.println(sCurrentLine);
start = start + 0.000100;
df.format(start);
i = i + 1;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
我正在尝试读取如下示例文件:
20659200000.000000,1.389320
20659200000.000000,1.318758
20659200000.000000,1.361239
20659200000.000000,1.379709
20659200000.000100,1.389320
20659200000.000100,1.357912
20659200000.000100,1.365672
20659200000.000100,1.374912
20659200000.000100,1.318937
20659200000.000100,1.355331
20659200000.000200,1.370660
20659200000.000200,1.365118
20659200000.000200,1.364933
我只想从左列中选择一个值(第一个)。
当前仅访问一次if条件
我更改了if条件和equals方法,现在它只适用于前三次迭代。更新开始值时出错
有什么想法吗?
谢谢。以下是您在阅读循环中应该做的事情:
while ((sCurrentLine = br.readLine()) != null) {
String[] tmp = sCurrentLine.split(",");
// here only use tmp[0] if you just want to pick the first column
// you could parse it to a double and calculate the stuff you want
System.out.println(tmp[0]);
}
以下是您在读取循环中应执行的操作:
while ((sCurrentLine = br.readLine()) != null) {
String[] tmp = sCurrentLine.split(",");
// here only use tmp[0] if you just want to pick the first column
// you could parse it to a double and calculate the stuff you want
System.out.println(tmp[0]);
}
以下是您在读取循环中应执行的操作:
while ((sCurrentLine = br.readLine()) != null) {
String[] tmp = sCurrentLine.split(",");
// here only use tmp[0] if you just want to pick the first column
// you could parse it to a double and calculate the stuff you want
System.out.println(tmp[0]);
}
以下是您在读取循环中应执行的操作:
while ((sCurrentLine = br.readLine()) != null) {
String[] tmp = sCurrentLine.split(",");
// here only use tmp[0] if you just want to pick the first column
// you could parse it to a double and calculate the stuff you want
System.out.println(tmp[0]);
}
您不能用这种方式比较两个
double
s。转换为double
时会出现精度损失,这意味着两个看似相等的值最终可能不同,而两个看似不同的值最终可能相同
您应该做的是将所需的值存储为字符串
,然后将读取的字符串
值与存储的值进行比较,或者使用BigDecimal.equals()
方法测试两个BigDecimal
实例之间的相等性
比较两个
String
s当然只有在您知道它们以完全相同的格式存储(相同的小数位数等)时才会起作用。比较两个BigDecimal
实例将起作用,前提是您的值可以精确地表示为BigDecimal
,并且因为您是从文本文件读取值,所以应该如此 您不能用这种方式比较两个double
s。转换为double
时会出现精度损失,这意味着两个看似相等的值最终可能不同,而两个看似不同的值最终可能相同
您应该做的是将所需的值存储为字符串
,然后将读取的字符串
值与存储的值进行比较,或者使用BigDecimal.equals()
方法测试两个BigDecimal
实例之间的相等性
比较两个
String
s当然只有在您知道它们以完全相同的格式存储(相同的小数位数等)时才会起作用。比较两个BigDecimal
实例将起作用,前提是您的值可以精确地表示为BigDecimal
,并且因为您是从文本文件读取值,所以应该如此 您不能用这种方式比较两个double
s。转换为double
时会出现精度损失,这意味着两个看似相等的值最终可能不同,而两个看似不同的值最终可能相同
您应该做的是将所需的值存储为字符串
,然后将读取的字符串
值与存储的值进行比较,或者使用BigDecimal.equals()
方法测试两个BigDecimal
实例之间的相等性
比较两个
String
s当然只有在您知道它们以完全相同的格式存储(相同的小数位数等)时才会起作用。比较两个BigDecimal
实例将起作用,前提是您的值可以精确地表示为BigDecimal
,并且因为您是从文本文件读取值,所以应该如此 您不能用这种方式比较两个double
s。转换为double
时会出现精度损失,这意味着两个看似相等的值最终可能不同,而两个看似不同的值最终可能相同
您应该做的是将所需的值存储为字符串
,然后将读取的字符串
值与存储的值进行比较,或者使用BigDecimal.equals()
方法测试两个BigDecimal
实例之间的相等性
比较两个
String
s当然只有在您知道它们以完全相同的格式存储(相同的小数位数等)时才会起作用。比较两个BigDecimal
实例将起作用,前提是您的值可以精确地表示为BigDecimal
,并且因为您是从文本文件读取值,所以应该如此 您应该在if语句中使用模数和增量
但是,为什么不直接使用
所以
您应该在if语句中使用模数和增量 但是,为什么不直接使用 所以
您应该在if语句中使用模数和增量 但是,为什么不直接使用 所以
您应该在if语句中使用模数和增量 但是,为什么不直接使用 所以
实际上这里的
if
条件将被访问一次,因为在您编写的tmp[i]
条件中。第一次迭代后,i
变为1。然后在下一次迭代中,tmp[i]值更改为1.318758
,这是第二行的第二列值
因此,该条件不满足并中断
你能做的就是把
inti=0在while
条件之后执行code>。实际上这里的如果
条件将被访问一次,则在您编写的tmp[i]
条件中执行。第一次迭代后,i
变为1。然后在下一次迭代中tmp[i]
值更改为