Java::比较舍入的双精度浮点数不起作用。

huangapple go评论78阅读模式
英文:

Java :: comparing ronded doubles doesn't work

问题

我想比较两个数字:

  • -3.123
  • -3.123456

我将使用 double 来存储它们,我只想考虑前3位小数,所以:

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DecimalComparator {
    public static void main(String[] args) {
        areEqualByThreeDecimalPlaces(-3.123, -3.123456);
    }

    public static boolean areEqualByThreeDecimalPlaces(double one, double two) {
        boolean same = true;

        DecimalFormat df = new DecimalFormat("#.###");
        df.setRoundingMode(RoundingMode.FLOOR);
        System.out.println(df.format(one));
        System.out.println(df.format(two));

        if (df.format(one).equals(df.format(two))) {
            same = true;
            System.out.println("true");
        } else {
            same = false;
            System.out.println("false");
        }
        return same;
    }
}

这段代码返回给我:

-3.123
-3.124
false

为什么第二个数字会四舍五入为 -3.124?

英文:

I want to compare two numbers:

  • -3.123
  • -3.123456

I will use double to store them and I just want to consider the first 3 decimals, so:

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DecimalComparator {
    public static void main(String[] args) {
        areEqualByThreeDecimalPlaces(-3.123, -3.123456);
    }

    public static boolean areEqualByThreeDecimalPlaces (double one, double two) {
        boolean same = true;

        DecimalFormat df = new DecimalFormat("#.###");
        df.setRoundingMode(RoundingMode.FLOOR);
        System.out.println(df.format(one));
        System.out.println(df.format(two));

        if (df.format(one).equals(df.format(two))) {
            same = true;
            System.out.println("true");
        } else {
            same = false;
            System.out.println("false");
        }
        return same;
    }
}

The code is returning me:

-3.123
-3.124
false

why the second number is rounding to -3.124?

答案1

得分: 1

    Hi its just that when u round off 3.123456 it works the below way when decimal
    places are reduced :

     3.12346
     3.1235
     3.124
     3.12
     3.1 

    // commenting the set rounding mode will make it work the way u want.
    public static void main (String[] args ) {
        double one =-3.123;
        double two = -3.123456;
        boolean same = true;

        DecimalFormat df = new DecimalFormat("#.###");
        // remove this line
        // df.setRoundingMode(RoundingMode.FLOOR);

        System.out.println(df.format(one));
        System.out.println(df.format(two));

        if (df.format(one).equals(df.format(two))) {
            same = true;
            System.out.println("true");
        } else {
            same = false;
            System.out.println("false");
        }
        System.out.println(same);
    }
英文:
Hi its just that when u round off 3.123456 it works the below way when decimal 
places are reduced :
 
 3.12346
 3.1235
 3.124
 3.12
 3.1 
 
//commenting the set rounding mode will make it work the way u want.
public static void main (String[] args ) {
        double one =-3.123;
         double two = -3.123456;
        boolean same = true;

        DecimalFormat df = new DecimalFormat("#.###");
        //remove this line
        //df.setRoundingMode(RoundingMode.FLOOR);
       
        System.out.println(df.format(one));
        System.out.println(df.format(two));

        if (df.format(one).equals(df.format(two))) {
            same = true;
            System.out.println("true");
        } else {
            same = false;
            System.out.println("false");
        }
        System.out.println(same);
    }

答案2

得分: 0

RoundingMode.FLOOR会将数字向下舍入 - 您的代码对于正数可以工作,但对于负数则不行。您需要使用 RoundingMode.DOWN ,它只是在第 N 位之后删除数字:

df.setRoundingMode(RoundingMode.DOWN);
// 在这里 ------------------------^
英文:

RoundingMode.FLOOR rounds the number towards the lower side - your code would work for positive numbers, but not for negative numbers. You need to use RoundingMode.DOWN that just removes digits after N places:

df.setRoundingMode(RoundingMode.DOWN);
// Here ------------------------^

huangapple
  • 本文由 发表于 2020年4月10日 16:44:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61136849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定