java double方法_Java Double类compare()方法与示例

java double方法

双类compare()方法 (Double class compare() method)

  • compare() method is available in java.lang package.

    compare()方法在java.lang包中可用。

  • compare() method is used to check equality or inequality of the given two double values or in other words, we can say this method is used to compare two double values.

    compare()方法用于检查给定两个双精度值的相等或不相等,换句话说,可以说此方法用于比较两个双精度值。

  • compare() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also, we will not get an error.

    compare()方法是一个静态方法,它也可以使用类名进行访问,如果我们尝试使用类对象访问该方法,那么也不会出错。

  • compare() method does not throw an exception at the time of comparing double values.

    比较双精度值时, compare()方法不会引发异常。

Syntax:

句法:

    public static int compare(double value1, double value2);

Parameter(s):

参数:

  • double value1, double value2 – These parameters represent the double values to be compared.

    double value1,double value2 –这些参数表示要比较的double值。

Return value:

返回值:

The return type of this method is int, it returns an integer value.

此方法的返回类型为int ,它返回一个整数值。

  • In the first case, it returns 0 if value1 is mathematically equals to value2.

    在第一种情况下,如果value1在数学上等于value2 ,则它返回0

  • In the second case, it returns the value < 0 if value1 is mathematically less than value2.

    在第二种情况下,如果value1在数学上小于value2 ,则它返回值<0

  • In the third case, it returns the value > 0 if value1 is mathematically greater than value2.

    在第三种情况下,如果value1在数学上大于value2 ,则它返回> 0的 值 。

Example:

例:

// Java program to demonstrate the example 
// of compare(double value1,double value2) 
// method of Double class

public class CompareOfDoubleClass {
    public static void main(String[] args) {
        // Variables initialization
        double value1 = 18.20;
        double value2 = 20.20;

        // It compares two double values and 
        // returns the result in another variable (compare) 
        // of integer types
        int compare = Double.compare(value1, value2);

        // Display result
        System.out.println("Double.compare(value1,value2): " + compare);
        System.out.println();

        if (compare == 0)
            System.out.println("value1 is equal to value2");
        else if (compare < 0)
            System.out.println("value1 is less than value2");
        else
            System.out.println("value1 is greater than value2");
    }
}

Output

输出量

Double.compare(value1,value2): -1

value1 is less than value2


翻译自: https://www.includehelp.com/java/double-class-compare-method-with-example.aspx

java double方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值