Home » Java programming language

Java Math Class static int round(float f) with example

Java Math Class static int round(float f) method: Here, we are going to learn about the static int round(float f) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 09, 2019

Math Class static int round(float f)

  • This method is available in java.lang package.
  • This method is used to return the nearest int value to the given argument and it is rounded to an integer by adding 1/2 and convert the result from float to int.
  • This is a static method, it is accessible with the class name too.
  • The return type of this method is int, it returns an integer which will be converted the value from float-floating to int by adding 1/2 of the given argument.
  • In this method, we pass only one parameter which represents a float number.
  • If the value of the given parameter after the decimal point is greater than 4 then the value is incremented by 1 before the decimal point is returned, else if the value of the given argument after the decimal point is less than or equal to 4 then the same value before the decimal point is returned.
  • This method does not throw any exception.

Syntax:

    public static int round(float f){
    }

Parameter(s): f – a float value whose nearest to integer value to be found.

Note:

  • If we pass "NaN" (Not a Number), it returns 0.
  • If we pass negative infinity, it returns the "Integer.MIN_VALUE".
  • If we pass positive infinity, it returns the "Integer.MAX_VALUE".
  • If we pass the value which is less than or equal to "Integer.MIN_VALUE", it returns "Integer.MIN_VALUE".
  • If we pass the value which is greater than or equal to "Integer.MAX_VALUE", it returns "Integer.MAX_VALUE".

Return value:

The return type of this method is int, it returns an integer value which is the nearest to integer value of given parameter.

Java program to demonstrate example of round(float f) method

// Java program to demonstrate the example of 
// round(float f) method of Math Class.

public class RintMethod {
    public static void main(String[] args) {
        // declaring the variables
        float f1 = -1.0f / 0.0f;
        float f2 = 1.0f / 0.0f;
        float f3 = 1234.56f;
        float f4 = 1234.42f;

        // Here , we will get (Integer.MIN_VALUE) and we are 
        // passing parameter whose value is (-Infinity)
        System.out.println("Math.round (f1): " + Math.round(f1));

        // Here , we will get (Integer.MAX_VALUE) and we are 
        // passing parameter whose value is (Infinity)
        System.out.println("Math.round (f2): " + Math.round(f2));

        // Here , we will get (1235) and we are 
        // passing parameter whose value is (1234.56)
        System.out.println("Math.round (f3): " + Math.round(f3));

        // Here , we will get (1234) and we are 
        // passing parameter whose value is (1234.12)
        System.out.println("Math.round (f4): " + Math.round(f4));
    }
}

Output

E:\Programs>javac RintMethod.java

E:\Programs>java RintMethod
Math.round (f1): -2147483648
Math.round (f2): 2147483647
Math.round (f3): 1235
Math.round (f4): 1234



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.