Java.lang Package Classes

Java StrictMath - copySign() Method



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

Syntax

public static float copySign(float magnitude, float 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(StrictMath.copySign(-324.1f, 4f));   
  System.out.println(StrictMath.copySign(500f, -21f));
  System.out.println(StrictMath.copySign(-40.2f, -15f));     
 }
}

The output of the above code will be:

324.1
-500.0
-40.2

❮ Java.lang - StrictMath