copySign() double in Java

copySign(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.copySign(double magnitude, double sign)

This method takes two arguments as its parameter of type double. This method returns the first floating-point argument with the sign of the second floating-point argument. 

Parameters: Two parameters are required for this method.

magnitude: the parameter providing the magnitude of the result.

sign: the parameter providing the sign of the result.

Returns: a value with the magnitude of magnitude and the sign of a sign.

Approach

Java

public class CopySignDouble {
    public static void main(String[] args) {

        double magnitude = 13;
        double sign = -1;

        System.out.println(Math.copySign(magnitude, sign));
    }
}

Output:

-13.0

No comments:

Post a Comment