The java.lang.Math.round() method is a built-in function that is used to round off the decimal numbers to the nearest value. This method is used to return the closest long to the argument, with ties rounding to positive infinity.

In this article, we will discuss the Math library in Java language. We will discuss how Java Math Roundmethod works in Java. We will make programs to understand Java Math Round method clearly. We will also discuss edge cases of Java Math Round method. Moving forward, letâ€™s understand about Math Library in Java.

What is Math Library in Java?

The Math library in Java provides functions that are helpful in performing mathematical operations. The Math library consists of the following methods:

Math Methods

The basic Math methods are used for square root, cube root, finding minimum and maximum numbers, and so on. For example, if you want the square root of a number, then you can use the Math.sqrt() function. Similarly, you can use Math.cbrt(), Math.max(), Math.min(), and so on.

Logarithmic Math Methods

The logarithmic math methods are used to return logarithmic value having data type double. For example, if you want to return logarithm of a double value, then you can use Math.log() method. Similarly, to return logarithm of a double value with base 10, you can use Math.log10() and so on.

Trigonometric Math Methods

Trigonometric Math Methods are used to return trigonometric sine, cosine, and so on. For example, if you want to return trigonometric sine value of any given double value, then you can use Math.sin() method. Similarly, to return trigonometric cosine value, Math.cos() can be used, and so on.

The Math library also supports Hyperbolic and Angular methods, like Math.sinh(), Math.cosh() are used to return hyperbolic sine and cosine values of any double value. And, Angular methods like Math.toDegrees() and Math.toRadians are used to convert Radians angel to Degree and Degree angels to Radians, respectively.

Moving forward, letâ€™s understand about Java Math Round method.

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

What is Math.round() Method in Java

The Java Math library provides a Math.round() method, which helps to round off decimal values to its nearest integer. The decimal values equal to or greater than .5 are changed to upper integer. Whereas decimal values less than .5 are changed to lower integer. For example, if we are printing round value of 2.5 or 2.6, then 3 will be printed, whereas for 2.4 or 2.3, 2 will be printed. See the below example to better understand.

Syntax of Math.round()

Syntax is used for rounding off the numbers in Java: Math.round()

round() Parameters

The Math.round() always need a single parameter. It can be of data type double and float. It is written as Math.round(double a) or Math.round(float a) a be any decimal.

round() Return Value

Math.round() always returns an integer value.

Implementation

Java

Java

class Solution {

public static void main(String[] args) {

System.out.println("Welcome to Coding Ninjas Studio Online Compiler!");

System.out.println("Round off of Number 2.5 is "+Math.round(2.5));

System.out.println("Round off of Number 2.6 is "+Math.round(2.6));

System.out.println("Round off of Number 2.4 is "+Math.round(2.4));

System.out.println("Round off of Number 2.3 is "+Math.round(2.3));

Math.round() with double returns the nearest long value. It can be written as: Syntex: Math.round(double a) where a is any number.

Implementation

Java

Java

class Solution {

public static void main(String[] args) { double a = 1220.9223; double b = -9578.1253;

System.out.println("Welcome to Coding Ninjas Studio Online Compiler!\n");

System.out.println("Math.round() for double "+ Math.round(a)); System.out.println("Math.round() for double "+ Math.round(b)); } }

Output

Example 3: Java Math.round() with float

Math.round() with double returns the nearest 'int' value. It can be written as: Syntex: Math.round(float a) where a is any number.

Implementation

Java

Java

class Solution { public static void main(String[] args) { float a = 20.93f; float b = -98.13f; System.out.println("Welcome to Coding Ninjas Studio Online Compiler!\n");

System.out.println("Math.round() for float "+ Math.round(a)); System.out.println("Math.round() for float "+ Math.round(b)); } }

Math.ceil() method returns the upper nearest value of a number.

How does the Math.round() method works?

The Math.round() method adds 0.5 to the number and then returns floor value of the number. For example, if we want Math.round() of 2.8, then Math.round function will add 0.5 to it and then return the floor value of 3.3 (2.8 + 0.5), which is 3.

How do you round to 2 decimal places in Java?

In general, if you want to round off any number in Java, use the Math.round() function, but if you want to round off up to a certain number of digits, in our instance, upto two decimal places, use the following syntax: Math.round(number*100)/100.

What is the return type of Math round in Java?

The return type of Math.round() in Java is long. It returns a long value representing the nearest integer to the argument, rounded to the nearest whole number.

How do you round down Math in Java?

The Math.floor() method returns the largest (closest to positive infinity) double value that is less than or equal to the argument.

Conclusion

In this article, we have discussed Java Math Round method and how we can use it.

To learn more about Java, refer to below-mentioned articles.

We hope this article has helped you in understanding the Java Math Round method. If this article helped you in any way, then you can read more such articles on our platform, Coding Ninjas Studio. You will find articles on almost every topic on our platform. Also, you can practice coding questions at Coding Ninjas to crack good product-based companies. For interview preparations, you can read the Interview Experiences of popular companies.