# Java StrictMath round() Method with Example

StrictMath Class round() method: Here, we are going to learn about the round() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 27, 2019

Syntax:

```    public static long round(double d);
public static int round(float f);
```

## StrictMath Class round() method

• round() method is available in java.lang package.
• round(double d) method is used to return the closest long value to the given argument.
• round(float f) method is used to return the nearest int value to the given argument and it is rounded to an integer by adding ½ and convert the result from float to int.
• These methods don't throw an exception.
• These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.

Parameter(s):

• float/double – represents the value to be rounded.

Return value:

The return type of this method is int / long – it returns the rounded value based on the given argument type.

Note:

• If we pass NaN, the method returns 0.
• If we pass a negative infinity, the method returns Long.MIN_VALUE.
• If we pass a positive infinity, the method returns Long.MAX_VALUE.
• If we pass a value which is less than or equal to Integer.MIN_VALUE/ Long.MIN_VALUE, the method returns Integer.MIN_VALUE/ Long.MIN_VALUE.
• If we pass a value which is greater than or Integer.MAX_VALUE/ Long.MAX_VALUE, the method returns Integer.MAX_VALUE/ Long.MAX_VALUE.

Example:

```// Java program to demonstrate the example
// of round() method of StrictMath class

public class Round {
public static void main(String[] args) {
// variable declarations
double d1 = -1.0 / 0.0;
double d2 = 1.0 / 0.0;
double d3 = 1234.56;
double d4 = 1234.42;

float f1 = -1.0f / 0.0f;
float f2 = 1.0f / 0.0f;
float f3 = 1234.56f;
float f4 = 1234.42f;

System.out.println();
System.out.println("round(double): ");

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

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

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

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

System.out.println();
System.out.println("round(float): ");

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

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

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

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

Output

```round(double):
StrictMath.round (d1): -9223372036854775808
StrictMath.round (d2): 9223372036854775807
StrictMath.round (d3): 1235
StrictMath.round (d4): 1234

round(float):
StrictMath. round (f1): -2147483648
StrictMath. round (f2): 2147483647
StrictMath. round (f3): 1235
StrictMath. round (f4): 1234
```

TOP Interview Coding Problems/Challenges

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates