Java Math copySign()方法及实例

来源:这里教程网 时间:2026-02-17 20:56:56 作者:

Java Math copySign()方法及实例

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

注意:

参数可以是两种类型。

double类型:copySign(double magt, double sign)float类型:copySign(float magt, float sign)

语法:

public static double copySign(DataType magt, DataType sign)

参数:
magt:提供结果的大小的参数。
sign:提供结果的符号的参数。
返回 :
该方法返回第一个参数的大小和第二个参数的符号。
第二个参数的符号。

例子:展示java.lang.Math.copySign()方法的工作。

// Java program to demonstrate working// of java.lang.Math.copySign() methodimport java.lang.Math;  class Gfg {      // driver code    public static void main(String args[])    {        double a = 34.543;        double b = -123.44;          // Input a, b        // Output -34.543( a- Magnitude, b- Sign)        System.out.println(Math.copySign(a, b));          // Input b, a        // Output 123.44( b- Magnitude, a- Sign)        System.out.println(Math.copySign(b, a));          float c = 87.56f;        float d = -685.23f;          // Input c, d        // Output -87.56( c- Magnitude, d- Sign)        System.out.println(Math.copySign(c, d));          // Input d, c        // Output 685.23( d- Magnitude, c- Sign)        System.out.println(Math.copySign(d, c));    }}

输出:

-34.543123.44-87.56685.23

相关推荐