![]() |
VOOZH | about |
The copySign() method in Java is a part of java.lang.Math class. This method copies the sign of one number to the magnitude of another. We can also say that it takes two numbers and returns a value with the absolute value of the first and the sign of the second.
This method is very useful in mathematical computations where controlling the sign of values is important. This method controls over the sign of a number without changing its absolute value.
This method comes in two forms:
public static double copySign(double magnitude, double sign)
public static float copySign(float magnitude, float sign)
Parameters:
Return Value:
The method returns a new value that has:
Why Use Math.copySign() Method?
Example 1: In this example, we will take double inputs where one value is positive and the other value is negative.
Result 1: -34.543 Result 2: 123.44
Example 2: In this example, we will use float values and apply the sign of a negative number to a positive float value.
Result 1: -87.56 Result 2: 685.23