Professional Documents
Culture Documents
Math Methods-Round
Math Methods-Round
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.
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 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.
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.
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