LogIn
I don't have account.

Java Math.round() Method

DevSniper
170 Views

public static int round(float x)  
public static long round(double x) 

In Programming, precise handling of numerical data especially when dealing with floating-point numbers. Java offers a variety of methods in its standard library to manage numeric operations effectively. One such method is Java Math.round. Java Math.round() is a in-build static method that is present in Java.lang.Math class. Java Math.round() method plays crucial role in rounding decimal numbers to the nearest integer.

  • If the numbers with a fractional part less than 0.5 are rounded down (towards the smaller integer).
  • If the numbers with a fractional part greater than or equal to 0.5 are rounded up (towards the larger integer).
  • If the parameter is a decimal value, either positive or negative, this method will return the nearest Integer/long value.
  • If the parameter is positive Infinity this method will return Integer.MAX_VALUE (float parameter) OR Long.MAX_VALUE (double parameter). Visit section Java Math.round With Positive Infinity.
  • If the parameter is negative Infinity this method will return Integer.MIN_VALUE (float parameter) OR Long.MIN_VALUE (double parameter) . visit section Java Math.round With Negative Infinity
  • If the parameter is NaN this method will return Zero.

Math.round With Positive Number

public class Program 
{
    public static void main(String []args) 
    {
        System.out.println("Math.round(10) =  "+ Math.round(10));
        System.out.println("Math.round(10L) =  "+ Math.round(10L));
        System.out.println("Math.round(10f) =  "+ Math.round(10f));
        System.out.println("Math.round(10.0) =  "+ Math.round(10.0));
        System.out.println("Math.round(9.7f) =  "+ Math.round(9.7f));
        System.out.println("Math.round(9.5f) =  "+ Math.round(9.5f));
        System.out.println("Math.round(9.1f) =  "+ Math.round(9.1f));
        System.out.println("Math.round(9.7) =  "+ Math.round(9.7));
        System.out.println("Math.round(9.5) =  "+ Math.round(9.5));
        System.out.println("Math.round(9.1) =  "+ Math.round(9.1));
    }
}
Math.round(10) =  10
Math.round(10L) =  10
Math.round(10f) =  10
Math.round(10.0) =  10
Math.round(9.7f) =  10
Math.round(9.5f) =  10
Math.round(9.1f) =  9
Math.round(9.7) =  10
Math.round(9.5) =  10
Math.round(9.1) =  9

Math.round With Negative Number

public class Program 
{
    public static void main(String []args) 
    {
        System.out.println("Math.round(-10) =  "+ Math.round(-10));
        System.out.println("Math.round(-10L) =  "+ Math.round(-10L));
        System.out.println("Math.round(-10f) =  "+ Math.round(-10f));
        System.out.println("Math.round(-10.0) =  "+ Math.round(-10.0));
        System.out.println("Math.round(-9.7f) =  "+ Math.round(-9.7f));
        System.out.println("Math.round(-9.5f) =  "+ Math.round(-9.5f));
        System.out.println("Math.round(-9.1f) =  "+ Math.round(-9.1f));
        System.out.println("Math.round(-9.7) =  "+ Math.round(-9.7));
        System.out.println("Math.round(-9.5) =  "+ Math.round(-9.5));
        System.out.println("Math.round(-9.1) =  "+ Math.round(-9.1));
    }
}
Math.round(-10) =  -10
Math.round(-10L) =  -10
Math.round(-10f) =  -10
Math.round(-10.0) =  -10
Math.round(-9.7f) =  -10
Math.round(-9.5f) =  -9
Math.round(-9.1f) =  -9
Math.round(-9.7) =  -10
Math.round(-9.5) =  -9
Math.round(-9.1) =  -9

Java Math.round With Positive Infinity

public class Program
{
    public static void main(String []args)
    {
        System.out.println("Math.round(10/0.0) =  "+ Math.round(10/0.0) );
        System.out.println("Math.round(10L/0.0) =  "+ Math.round(10L/0.0) );
        System.out.println("Math.round(10f/0.0) =  "+ Math.round(10/0.0) );
        System.out.println("Math.round(10.1/0.0) =  "+ Math.round(10.1/0.0) );
        System.out.println("Math.round(10f/0) =  "+ Math.round(10f/0) );
        System.out.println("Math.round(10f/-0) =  "+ Math.round(10f/-0) );
        System.out.println("Math.round(-10/-0f) =  "+ Math.round(-10/-0f) );
        System.out.println("Math.round(10.1/0) =  "+ Math.round(10.1/0) );
        System.out.println("Math.round(10.1/-0) =  "+ Math.round(10.1/-0) );
        System.out.println("Math.round(-10.1/-0.0) =  "+ Math.round(-10/-0.0) );
    }
}
Math.round(10/0.0) =  9223372036854775807
Math.round(10L/0.0) =  9223372036854775807
Math.round(10f/0.0) =  9223372036854775807
Math.round(10.1/0.0) =  9223372036854775807
Math.round(10f/0) =  2147483647
Math.round(10f/-0) =  2147483647
Math.round(-10/-0f) =  2147483647
Math.round(10.1/0) =  9223372036854775807
Math.round(10.1/-0) =  9223372036854775807
Math.round(-10.1/-0.0) =  9223372036854775807

Java Math.round With Negative Infinity

public class Program 
{
    public static void main(String []args)
    {
        System.out.println("Math.round(10/-0.0) =  "+ Math.round(10/-0.0) );
        System.out.println("Math.round(10L/-0.0) =  "+ Math.round(10L/-0.0) );
        System.out.println("Math.round(10f/-0.0) =  "+ Math.round(10f/-0.0) );
        System.out.println("Math.round(-10f/0.0) =  "+ Math.round(-10f/0.0) );
        System.out.println("Math.round(-10f/0) =  "+ Math.round(-10f/0) );
        System.out.println("Math.round(10.1/-0.0) =  "+ Math.round(10.1/-0.0) );
        System.out.println("Math.round(-10.1/0.0) =  "+ Math.round(-10.1/0.0) );
        System.out.println("Math.round(-10.1/0) =  "+ Math.round(-10.1/0) );
    }
}
Math.round(10/-0.0) =  -9223372036854775808
Math.round(10L/-0.0) =  -9223372036854775808
Math.round(10f/-0.0) =  -9223372036854775808
Math.round(-10f/0.0) =  -9223372036854775808
Math.round(-10f/0) =  -2147483648
Math.round(10.1/-0.0) =  -9223372036854775808
Math.round(-10.1/0.0) =  -9223372036854775808
Math.round(-10.1/0) =  -9223372036854775808

Java Math.round With NaN

public class Program 
{
    public static void main(String []args) 
    {
        System.out.println("Math.round(0/0.0) =  "+ Math.round(0/0.0) );
        System.out.println("Math.round(0/-0.0) =  "+ Math.round(0/-0.0) );
        System.out.println("Math.round(-0/0.0) =  "+ Math.round(-0/0.0) );
        System.out.println("Math.round(-0/-0.0) =  "+ Math.round(-0/-0.0) );
        System.out.println("Math.round(0/0f) =  "+ Math.round(0/0f) );
        System.out.println("Math.round(0/-0f) =  "+ Math.round(0/-0f) );
        System.out.println("Math.round(-0/0f) =  "+ Math.round(-0/0f) );
        System.out.println("Math.round(-0/-0f) =  "+ Math.round(-0/-0f) );
    }
}
Math.round(0/0.0) =  0
Math.round(0/-0.0) =  0
Math.round(-0/0.0) =  0
Math.round(-0/-0.0) =  0
Math.round(0/0f) =  0
Math.round(0/-0f) =  0
Math.round(-0/0f) =  0
Math.round(-0/-0f) =  0

Java Math.round Behavior With Integer.MIN_VALUE ,Integer.MAX_VALUE, Long.MIN_VALUE and Long.MAX_VALUE

Long.MIN_VALUE = -9223372036854775808
Integer.MIN_VALUE = -2147483648
Long.MAX_VALUE = 9223372036854775807
Integer.MAX_VALUE = 2147483647

public class Program 
{
    public static void main(String []args)
    {
        System.out.println("Math.round(Long.MIN_VALUE) = "+Math.round(Long.MIN_VALUE));
        System.out.println("Math.round(Integer.MIN_VALUE) = "+Math.round(Integer.MIN_VALUE));
        System.out.println("Math.round(Long.MAX_VALUE) = "+Math.round(Long.MAX_VALUE));
        System.out.println("Math.round(Integer.MAX_VALUE) = "+Math.round(Integer.MAX_VALUE));
        
        System.out.println("Math.round((long)Long.MIN_VALUE) = "+Math.round((long)Long.MIN_VALUE));
        System.out.println("Math.round((long)Integer.MIN_VALUE) = "+Math.round((long)Integer.MIN_VALUE));
        System.out.println("Math.round((long)Long.MAX_VALUE) = "+Math.round((long)Long.MAX_VALUE));
        System.out.println("Math.round((long)Integer.MAX_VALUE) = "+Math.round((long)Integer.MAX_VALUE));
        
        System.out.println("Math.round((float)Long.MIN_VALUE) = "+Math.round((float)Long.MIN_VALUE));
        System.out.println("Math.round((float)Integer.MIN_VALUE) = "+Math.round((float)Integer.MIN_VALUE));
        System.out.println("Math.round((float)Long.MAX_VALUE) = "+Math.round((float)Long.MAX_VALUE));
        System.out.println("Math.round((float)Integer.MAX_VALUE) = "+Math.round((float)Integer.MAX_VALUE));
        
        System.out.println("Math.round((double)Long.MIN_VALUE) = "+Math.round((double)Long.MIN_VALUE));
        System.out.println("Math.round((double)Integer.MIN_VALUE) = "+Math.round((double)Integer.MIN_VALUE));
        System.out.println("Math.round((double)Long.MAX_VALUE) = "+Math.round((double)Long.MAX_VALUE));
        System.out.println("Math.round((double)Integer.MAX_VALUE) = "+Math.round((double)Integer.MAX_VALUE));
    }
}
Math.round(Long.MIN_VALUE) = -2147483648
Math.round(Integer.MIN_VALUE) = -2147483648
Math.round(Long.MAX_VALUE) = 2147483647
Math.round(Integer.MAX_VALUE) = 2147483647
Math.round((long)Long.MIN_VALUE) = -2147483648
Math.round((long)Integer.MIN_VALUE) = -2147483648
Math.round((long)Long.MAX_VALUE) = 2147483647
Math.round((long)Integer.MAX_VALUE) = 2147483647
Math.round((float)Long.MIN_VALUE) = -2147483648
Math.round((float)Integer.MIN_VALUE) = -2147483648
Math.round((float)Long.MAX_VALUE) = 2147483647
Math.round((float)Integer.MAX_VALUE) = 2147483647
Math.round((double)Long.MIN_VALUE) = -9223372036854775808
Math.round((double)Integer.MIN_VALUE) = -2147483648
Math.round((double)Long.MAX_VALUE) = 9223372036854775807
Math.round((double)Integer.MAX_VALUE) = 2147483647

Java Math.round With Integer.MIN_VALUE

public class Program 
{
    public static void main(String []args) 
    {
        System.out.println("Integer.MIN_VALUE = "+Integer.MIN_VALUE);
        System.out.println("Integer.MAX_VALUE = "+Integer.MAX_VALUE);
        System.out.println("Math.round(Integer.MIN_VALUE) = "+Math.round(Integer.MIN_VALUE));
        System.out.println("Math.round((long)Integer.MIN_VALUE) = "+Math.round((long)Integer.MIN_VALUE));
        System.out.println("Math.round((float)Integer.MIN_VALUE) = "+Math.round((float)Integer.MIN_VALUE));
        System.out.println("Math.round((double)Integer.MIN_VALUE) = "+Math.round((double)Integer.MIN_VALUE));
        
        System.out.println("Integer.MIN_VALUE -1 = "+(Integer.MIN_VALUE-1));
        System.out.println("Math.round(Integer.MIN_VALUE-1) = "+Math.round((Integer.MIN_VALUE-1)));
        System.out.println("Math.round((long)(Integer.MIN_VALUE-1)) = "+Math.round((long)(Integer.MIN_VALUE-1)));
        System.out.println("Math.round((float)(Integer.MIN_VALUE-1)) = "+Math.round((float)(Integer.MIN_VALUE-1)));
        System.out.println("Math.round((double)(Integer.MIN_VALUE-1)) = "+Math.round((double)(Integer.MIN_VALUE-1)));
        
        System.out.println("Integer.MIN_VALUE -1.0 = "+(Integer.MIN_VALUE-1.0));
        System.out.println("Math.round(Integer.MIN_VALUE-1.0) = "+Math.round((Integer.MIN_VALUE-1.0)));
        System.out.println("Math.round((long)(Integer.MIN_VALUE-1.0)) = "+Math.round((long)(Integer.MIN_VALUE-1.0)));
        System.out.println("Math.round((float)(Integer.MIN_VALUE-1.0)) = "+Math.round((float)(Integer.MIN_VALUE-1.0)));
        System.out.println("Math.round((double)(Integer.MIN_VALUE-1.0)) = "+Math.round((double)(Integer.MIN_VALUE-1.0)));
        
        System.out.println("Integer.MIN_VALUE -5 = "+(Integer.MIN_VALUE-5));
        System.out.println("Math.round(Integer.MIN_VALUE-5) = "+Math.round((Integer.MIN_VALUE-5)));
        System.out.println("Math.round((long)(Integer.MIN_VALUE-5)) = "+Math.round((long)(Integer.MIN_VALUE-5)));
        System.out.println("Math.round((float)(Integer.MIN_VALUE-5)) = "+Math.round((float)(Integer.MIN_VALUE-5)));
        System.out.println("Math.round((double)(Integer.MIN_VALUE-5)) = "+Math.round((double)(Integer.MIN_VALUE-5)));

    }
}
Integer.MIN_VALUE = -2147483648
Integer.MAX_VALUE = 2147483647
Math.round(Integer.MIN_VALUE) = -2147483648
Math.round((long)Integer.MIN_VALUE) = -2147483648
Math.round((float)Integer.MIN_VALUE) = -2147483648
Math.round((double)Integer.MIN_VALUE) = -2147483648
Integer.MIN_VALUE -1 = 2147483647
Math.round(Integer.MIN_VALUE-1) = 2147483647
Math.round((long)(Integer.MIN_VALUE-1)) = 2147483647
Math.round((float)(Integer.MIN_VALUE-1)) = 2147483647
Math.round((double)(Integer.MIN_VALUE-1)) = 2147483647
Integer.MIN_VALUE -1.0 = -2.147483649E9
Math.round(Integer.MIN_VALUE-1.0) = -2147483649
Math.round((long)(Integer.MIN_VALUE-1.0)) = -2147483648
Math.round((float)(Integer.MIN_VALUE-1.0)) = -2147483648
Math.round((double)(Integer.MIN_VALUE-1.0)) = -2147483649
Integer.MIN_VALUE -5 = 2147483643
Math.round(Integer.MIN_VALUE-5) = 2147483647
Math.round((long)(Integer.MIN_VALUE-5)) = 2147483647
Math.round((float)(Integer.MIN_VALUE-5)) = 2147483647
Math.round((double)(Integer.MIN_VALUE-5)) = 2147483643