【问题标题】:Comparing decimal values in java比较java中的十进制值
【发布时间】:2015-01-01 21:36:09
【问题描述】:

我正在尝试从逗号分隔的 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 方法,现在它只适用于前三个迭代。更新起始值时出错。

有什么想法吗? 谢谢。

【问题讨论】:

  • 请澄清您的问题。究竟是什么不工作?
  • @dudeprgm 更新了 Q。

标签: java scientific-notation


【解决方案1】:

这是您在读取循环中应该执行的操作:

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]);
}

【讨论】:

    【解决方案2】:

    你不能用这种方式比较两个doubles。转换为 double 时会损失精度,这意味着看起来相等的两个值最终可能不同,而看起来不同的两个值最终可能相同。

    您应该做的是将您想要的值存储为String,然后将您正在读取的String 值与您存储的值进行比较,或者使用BigDecimal.equals() 方法来测试两个 BigDecimal 实例之间的相等性。

    比较两个Strings 只有在您知道它们以完全相同的格式存储时才有效,当然(相同的小数位数等等)。比较两个 BigDecimal 实例将起作用,前提是您的值可以完全表示为 BigDecimal,并且由于您是从文本文件中读取值,因此确实应该如此!

    【讨论】:

      【解决方案3】:

      实际上这里if 条件将被访问一次,因为在您编写tmp[i] 的条件中。第一次迭代后,i 变为 1。然后在下一次迭代时,tmp[i] 值变为1.318758,即第二行的第二列值。

      所以条件不满足而中断。

      您可以将int i = 0; 放在while 条件之后。

      【讨论】:

        【解决方案4】:

        您应该在 if 语句中使用模数和增量。

        但是,为什么不直接使用访问第一列

        所以...

        ` while ((sCurrentLine = br.readLine()) != null) {
        
              String[] tmp = sCurrentLine.split(",");
              if(tmp.length > 0){
                     System.out.println("The first column is: " + tmp[0]);
              }
        }`
        

        【讨论】:

          【解决方案5】:

          最好走这条路:

                  String start = "20659200000.000000";
                  while ((sCurrentLine = br.readLine()) != null) {
                      String[] tmp = sCurrentLine.split(",");
                      if(tmp[0].equals(start)) {
                          System.out.println("Inside if");
                      }
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-10-24
            • 1970-01-01
            • 1970-01-01
            • 2010-09-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-26
            相关资源
            最近更新 更多