copySign() float in Java

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

Syntax:

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

This method takes two parameters of type float. 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.

For Example:

Math.copySign(13.5,-1) =  It returns -13.5.

Approach

Java

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

        float magnitude = (float13.5;
        float sign = -1;

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

    }
}

Output:

-13.5

No comments:

Post a Comment