static float copySign(float magnitude, float sign)
描述 (Description)
java.lang.Math.copySign(float magnitude, float sign)返回带有第二个浮点参数符号的第一个浮点参数。
声明 (Declaration)
以下是java.lang.Math.copySign()方法的声明
public static double copySign(float magnitude, float sign)
参数 (Parameters)
magnitude - 提供结果magnitude的参数
sign - 提供结果sign的参数
返回值 (Return Value)
此方法返回一个具有幅度值和符号符号的值。
异常 (Exception)
NA
例子 (Example)
以下示例显示了lang.Math.copySign()方法的用法。
package com.iowiki;
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