当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Math.copySign()用法及代码示例


java.lang.Math.copySign() 用于返回第一个参数和第二个参数的符号。

用法

public static double copySign(double a, double b)
public static float copySign(float a, float b)

参数

a = the argument providing the magnitude of the result
b = the argument providing the sign of the result

返回

This method returns the magnitude of the first argument with the sign of the second argument.

例子1

public class CopySignExample1
{
    public static void main(String[] args) 
    {
        double x = 740.4;
        double y = -29.1;
        // return -740.4 because second argument is negative  
        System.out.println(Math.copySign(x, y));
    }
}

输出:

-740.4

例子2

public class CopySignExample2
{
    public static void main(String[] args) 
    {
        double x = -580.73;
        double y = 26.3;
        // return 580.73 because second argument y is positive
        System.out.println(Math.copySign(x, y));
    }
}

输出:

580.73

例子3

public class CopySignExample3
{
    public static void main(String[] args) 
    {
        float x = -788.63f;
        float y = 26.9f;
        // return -26.9 because second argument x is negative
        System.out.println(Math.copySign(y, x));
    }
}

输出:

-26.9






相关用法


注:本文由纯净天空筛选整理自 Java Math.copySign() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。