Java.lang.Math.copySign() 方法
描述
java.lang.Math.copySign(float mass, float sign) 返回第一个浮点参数和第二个浮点参数的符号。
声明
以下是 java.lang.Math.copySign() 方法的声明。
public static double copySign(float magnitude, float sign)
参数
magnitude − 提供结果大小的参数
sign − 提供结果符号的参数
返回值
该方法返回一个带有大小和符号的值。
异常
NA
示例
下面的例子展示了 lang.Math.copySign() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class MathDemo {
public static void main(String[] args) {
// get two float numbers
float x = 125.9f;
float y = -0.4873f;
// print a double with the magnitude of x and the sign of y
System.out.println("Math.copySign(" + x + "," + y + ")=" + Math.copySign(x, y));
// print a double with the magnitude of y and the sign of x
System.out.println("Math.copySign(" + y + "," + x + ")=" + Math.copySign(y, x));
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Math.copySign(125.9f, -0.4873f)=-125.9 Math.copySign(-0.4873f, 125.9f)=0.4873

