Java中double 0.0是否大于或小于0 [英] Is double 0.0 is greater or less than 0 in java

查看:2752
本文介绍了Java中double 0.0是否大于或小于0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要求更多的澄清。这里是我的示例程序

I would like to ask for more clarification. Here my sample program

double diff = 7.500 - 7.500;
System.out.println(diff); // result 0.0
if (diff > 0) {
    System.out.println("+" + diff ); //result +0.0
} else {
    System.out.println("-" + diff ); //result -0.0
}

我的结果是 -0.0 。我的期望是0 == 0.0,然后跳过其他情况。但这会进入else条件。双精度0.0是大于还是小于0?

My result is -0.0. My expectation is 0 == 0.0 then skip if else condition. But it enter to the else condition.Is double 0.0 is greater or less than 0?

推荐答案

这是我的答案:

double diff = 7.500 - 7.500;
    System.out.println(diff);

    if(diff>0){
        System.out.println("+"+diff);
    }else if(diff<0){
        System.out.println("-"+diff);
    }else if(diff==0){
        System.out.println("="+diff);
    }
}

我的结果是这样的:

0.0

= 0.0

and my result is this :
0.0
=0.0

您必须忘记0.0等于0是'else the 0.0> 0'以外的'0.0> = 0 '

you must forget that the 0.0 equal 0 is 'else the 0.0 > 0' other the '0.0 >= 0'

这篇关于Java中double 0.0是否大于或小于0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆