Java.lang Package Classes

Java Math - copySign() Method



The java.lang.Math.copySign() method returns a number with magnitude of first argument and sign of second argument.

Syntax

public static double copySign(double magnitude, double sign)

Parameters

magnitude Specify a value providing the magnitude of the result.
sign Specify a value providing the sign of the result.

Return Value

Returns a number with magnitude of first argument and sign of second argument.

Exception

NA.

Example:

In the example below, copySign() method returns a number with magnitude of first argument and sign of second argument.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.copySign(-324.1, 4));   
  System.out.println(Math.copySign(500, -21));
  System.out.println(Math.copySign(-40.2, -15));     
 }
}

The output of the above code will be:

324.1
-500.0
-40.2

❮ Java.lang - Math