Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

Math Methods

Math.round()
This method returns the value of a number rounded to its
nearest integer. If the fractional part is less than 0.5 then it
returns the same integer value, otherwise it returns the next
higher integer. It gives different output for positive and
negative numbers and it returns the value in long/int.

General Syntax : <Return data type><variable> =


<Function name(Argument)>;
Example : double r=Math.round(-8.5);
Output : 8
Example : double r=Math.round(4.5);
Output : 5
Math Methods
Math.round()

If the argument is positive or negative number, this method


will return the nearest value.

If the argument is not a number (NaN), this method will


return Zero.

If the argument is positive Infinity, this method will


return Long.MIN_VALUE.

If the argument is negative Infinity, this method will


return Long.MAX_VALUE.
Math Methods
Example

public class RoundExample1  
{  
    public static void main(String[] args)   
    {  
        double x = 79.52;  
        // find the closest int for the double  
        System.out.println(Math.round(x));  
    }  

Math Methods
Math Methods
Example

public class RoundExample2  
{  
    public static void main(String[] args)   
    {  
        double x = -83.76;  
        // find the closest int for the double  
        System.out.println(Math.round(x));    
    }  
}
Math Methods
Math Methods
Example

public class RoundExample2


{
public static void main(String[] args)
{
double x = -83.15;
// find the closest int for the double
System.out.println(Math.round(x));
}
}
Math Methods
Math Methods
Example

public class RoundExample3


{
public static void main(String[] args)
{
double n = -1.0/0;
System.out.println(Math.round(n));
}
}
Math Methods
Math Methods
Example

public class RoundExample4  
{  
    public static void main(String[] args)   
    {  
        double x = 1.0/0;    
        System.out.println(Math.round(x));    
    }  

Math Methods
Math Methods
Example

public class RoundExample5  
{  
    public static void main(String[] args)   
    {  
        double x = 0.0/0;  
        // Input NaN, Output Zero  
        System.out.println(Math.round(x));    
    }  
}  
Math Methods
 
Math Methods
Math.rint()
This function returns the nearest integer of a given fractional
number. The return value data type will always be double.

General Syntax : <Return data type><variable> =


<Function name(Argument)>;

Example : double i=Math.rint(9.5);


Output : 9.0
Example : double i=Math.rint(-6.5);
Output : -7.0
Math Methods
Math.rint()

If the argument is positive or negative number, this method


will return the nearest value.

If two double values that are mathematical integers are


equally close, this method will return integer value that is
even.

If the argument is NaN or infinity or positive zero or


negative zero, this method will return the argument value as
a result.
Math Methods
Example

public class RintExample1  
{  
    public static void main(String[] args)   
    {  
        double x = 81.68;  
        // Input positive value, Output round the x  
        System.out.println(Math.rint(x));  
    }  

Math Methods
Math Methods
Example

public class RintExample2  
{  
    public static void main(String[] args)   
    {  
        double x = -37.25;  
        // Input negative zero, Output round the x    
        System.out.println(Math.rint(x));  
    }  
}  
Math Methods
Math Methods
Example

public class RintExample3  
{  
    public static void main(String[] args)   
    {  
        double x = 80.5;  
        double y = 79.5;      
        System.out.println(Math.rint(x));  
        System.out.println(Math.rint(y));  
    }  
}
  
Math Methods
  
Math Methods
Example

public class RintExample4  
{  
    public static void main(String[] args)   
    {  
        double x = 1.0/0;  
      // Input positive infinity, Output positive infinity  
        System.out.println(Math.rint(x));  
    }  

  
Math Methods
  
Math Methods
Math.ceil()
Ceil Round a number upward to its nearest integer. It always
returns the value as a double data type.

General Syntax : <Return data type><variable> =


<Function name(Argument)>;

Example : double c=Math.ceil(3.5);


Output : 4.0
Example : double c=Math.ceil(-3.5);
Output : -3.0
Math Methods
Math.ceil()

If the argument is positive or negative double value, this


method will return the ceil value.

If the argument is NaN, this method will return same


argument.

If the argument is Infinity, this method will


return Infinity with the same sign as the argument.
Math Methods
Math.ceil()

If the argument is positive or negative Zero, this method


will return Zero with same sign as the argument.

If the argument is less than Zero but greater than -1.0, this
method will return Negative Zero as output.
Math Methods
Example

public class CeilExample1  
{  
    public static void main(String[] args)   
    {  
        double x = 83.56;  
        // Input positive value, Output ceil value of x   
        System.out.println(Math.ceil(x));  
    }  

Math Methods
Math Methods
Example

public class CeilExample1  
{  
    public static void main(String[] args)   
    {  
        double x = 83.12;  
        // Input positive value, Output ceil value of x   
        System.out.println(Math.ceil(x));  
    }  
}
Math Methods
Math Methods
Example

public class CeilExample2  
{  
    public static void main(String[] args)   
    {  
        double x = -94.73;  
        // Input negative value, Output ceil value of x
        System.out.println(Math.ceil(x));  
    }  
}
Math Methods
Math Methods
Example

public class CeilExample3  
{  
    public static void main(String[] args)   
    {  
        double x = -1.0 / 0;  
   // Input negative infinity, Output negative infinity
        System.out.println(Math.ceil(x));  
    }  
}  
Math Methods
Math Methods
Example

public class CeilExample4  
{  
    public static void main(String[] args)   
    {  
        double x = 0.0;  
        // Input positive zero, Output positive zero  
        System.out.println(Math.ceil(x));  
    }  
}  
Math Methods
 
Math Methods
Example

public class CeilExample5  
{  
    public static void main(String[] args)   
    {  
        double x = -0.25;  
        System.out.println(Math.ceil(x));  
    }  
}
 
Math Methods
 

You might also like