LogIn
I don't have account.

Java Math.round() Method

DevSniper

175 Views
Copy
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

We demonstrate how the Math.round() method behaves when applied to positive integers, floats and doubles in Java. The code below prints the rounded results of various positive values like 10, 9.7, 9.5 and 9.1

This helps you clearly understand how Java’s rounding logic works values with decimals ≥ 0.5 are rounded up to the next whole number, while values below 0.5 are rounded down to the nearest integer.

Copy
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

When working with negative numbers, the behavior of Math.round() in Java might surprise you a bit. Unlike positive values where .5 always rounds up, for negative numbers, .5 behaves differently it rounds toward zero.

Copy
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

When a number becomes positive infinity (+∞), Java represents it as a special constant not a finite number. This usually happens when you divide a positive number by 0.0.

Copy
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
  • When the argument is positive infinity, Math.round() returns
    • Long.MAX_VALUE -> 9223372036854775807 (for double input)
    • Integer.MAX_VALUE -> 2147483647 (for float input)
  • This is because the rounding function cannot handle infinite values, so it returns the largest representable finite value for that type.

Java Math.round With Negative Infinity

Negative infinity (-∞) occurs when dividing a positive number by -0.0 or a negative number by 0.0.

Copy
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
  • When the argument is negative infinity, Java returns
    • Long.MIN_VALUE -> -9223372036854775808
    • Integer.MIN_VALUE -> -2147483648
  • Just like positive infinity, rounding can’t apply, so it picks the most negative possible value.

Java Math.round With NaN

NaN occurs when an undefined operation is performed, such as dividing 0 / 0.0 or taking the square root of a negative number.

Copy
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
  • For NaN (Not a Number) values, Math.round() always returns 0.
  • This behavior is consistent for both float and double inputs.
  • The reasoning is that NaN cannot be rounded, so Java defaults to zero as a neutral fallback.

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

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

Copy
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

Copy
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