gpt4 book ai didi

java - java中比较十进制值

转载 作者:行者123 更新时间:2023-12-01 22:31:07 25 4
gpt4 key购买 nike

我正在尝试从逗号分隔的 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;
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]);
}

关于java - java中比较十进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27734910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com