Home >>Java Math Methods >Java Math.copySign() method

Java Math.copySign() method

Java Math.copySign() method

The java.lang. Math.copySign() in Java Math is used to return the second argument with the sign.

Syntax

public static double copySign(double a, double b)  
public static float copySign(float a, float b)  

Parameters

a = This parameter is used the argument providing the magnitude of the result

b = This parameter is used the argument providing the sign of the result

Returns

This method is used to returns the magnitude of the first argument with the sign of the second argument.

Java Math.copySign() Method Example 1


public class MyClass  
{  
    public static void main(String[] args)   
    {  
        double x = 120.8;  
        double y = -34.4;  
        System.out.println(Math.copySign(x, y));  
    }  
}  

Output:
-120.8

Java Math.copySign() Method Example 2


public class MyClass 
{  
    public static void main(String[] args)   
    {  
        double x = -123.03;  
        double y = 16.2;  
        System.out.println(Math.copySign(x, y));  
    }  
}  

Output:
123.03

No Sidebar ads