简体   繁体   English

在Java中将两个浮点数与舍入进行比较(Big Java Ex 5.3)

[英]Comparing Two floating numbers with rounding in Java ( Big Java Ex 5.3)

I've read a bunch of questions on rounding numbers to n amount of decimal places and then I've found out using BigDecimal is pretty good However I'm having a bit of trouble with the following exercise in my book, Consider the following input and output 我已经读过很多关于将数字四舍五入到小数点后n位的问题,然后发现使用BigDecimal很好,但是我的书中的以下练习存在一些麻烦,请考虑以下输入和输出

Enter two floating-point numbers:
2.0
1.99998

//output
They are the same when rounded to two decimal places.
They differ by less than 0.01.

Enter two floating-point numbers:
0.999
0.991

//output
They are different when rounded to two decimal places.
They differ by less than 0.01.

MY CODE 我的密码

public class test{
    public static void main(String [] args ){
        Scanner getNum = new Scanner(System.in);
        Scanner getNumTwo = new Scanner(System.in);

        BigDecimal a = getNum.nextBigDecimal();
        float b = getNum.nextFloat();

        BigDecimal valBNew = new BigDecimal(b).setScale(2,BigDecimal.ROUND_HALF_UP);
        BigDecimal valANew = a;
        System.out.println(valBNew); // 2.00
        System.out.println(a); //2.0
        if (valBNew == a){
            System.out.println("They are the same when rounded to two decimal places");
        }
        else{
            System.out.println("They are different when rounded to two decimal places");
        }

    }
}

Heres my attempt. 这是我的尝试。 I'm having trouble using two floats and then using big decimal. 我在使用两个浮点数然后使用大十进制时遇到麻烦。 And then i'm having issues with my if statement since 2.0 isnt the same as 2.00 . 然后我的if语句出现了问题,因为2.02.00

Whats my best option here? 我最好的选择是什么? I'd like to get the rounding part working then the difference will be easy. 我想使舍入部分起作用,那么区别将很容易。

From the BigDecimal documentation . 来自BigDecimal文档

public int compareTo(BigDecimal val)

Compares this BigDecimal with the specified BigDecimal . 将此BigDecimal与指定的BigDecimal Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. 此方法将值相等但比例不同(例如2.0和2.00)的两个BigDecimal对象视为相等。

Whereas, for .equals .equals

public boolean equals(Object x)

Compares this BigDecimal with the specified Object for equality. 将此BigDecimal与指定的Object是否相等。 Unlike compareTo , this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method). compareTo不同,此方法仅在两个BigDecimal对象的值和比例相等时才认为它们相等(由此方法比较时,2.0不等于2.00)。

Therefore, this is correct 因此,这是正确的

if (valBNew.compareTo(a) == 0) {

}

If a and b are both floating point types, then 如果ab都是浮点类型,则

(long)(a * 100 + 0.5) == (long)(b * 100 + 0.5)

is an adequate test for 2-decimal-place-rounding equality, subject to your not overflowing the long type, and your being able to tolerate the limitations of binary floating point. 对于2小数位舍入相等性是一个足够的测试,条件是您不溢出long类型,并且您能够容忍二进制浮点数的限制。 The 0.5 constants implement the German rounding. 0.5常数实现德国舍入。

Based on @cricket_007 -s answer, this is how a clean solution would look like: 基于@ cricket_007 -s答案,这是一个干净的解决方案:

public class Test {
    public static void main(String [] args ) {
        try (Scanner getNum = new Scanner(System.in)) { // try-with-resources, e.g. it auto-closes the scanner
            double a = Double.parseDouble(getNum.next()); // parsing a number from a line
            double b = Double.parseDouble(getNum.next());

            BigDecimal valBNew = new BigDecimal(b); // 2.00, no rounding
            BigDecimal valANew = new BigDecimal(a); // 2.0
            if (valBNew.compareTo(valANew) == 0) { // this is the magic
                System.out.println("They are the same");
            } else {
                System.out.println("They are different");
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM