當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。