Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
1.
Introduction
2.
What is Math Library in Java?
2.1.
Math Methods
2.2.
Logarithmic Math Methods
2.3.
Trigonometric Math Methods
3.
What is Math.round() Method in Java
3.1.
Syntax of Math.round()
3.2.
round() Parameters
3.3.
round() Return Value
3.4.
Implementation
3.5.
Java
4.
Different Ways to Round Off in Java
5.
Example 1: Edge Cases of Math.round() Method
5.1.
Java
5.2.
Java
5.3.
Java
6.
Example 2: Java Math.round() with double
6.1.
Java
7.
Example 3: Java Math.round() with float
7.1.
Java
8.
8.1.
What is the use of Math.ceil() method?
8.2.
How does the Math.round() method works?
8.3.
How do you round to 2 decimal places in Java?
8.4.
What is the return type of Math round in Java?
8.5.
How do you round down Math in Java?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Java Math.round() method with Examples

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

## Introduction

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 Round method 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.

Also see, Swap Function in Java

Get the tech career you deserve, faster!
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.

• 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));    }}``

Output

To execute java programs, you can go to our Online Java Compiler - Coding Ninjas Coding Ninjas Studio.

## Different Ways to Round Off in Java

In Java, there are different ways to round off numbers using various methods available in the java.lang.Math class. Here are some common approaches:

Math.round() method:

• Uses the Math.round() method to round a floating-point number to the nearest integer.

DecimalFormat class:

• Utilizes the DecimalFormat class for more flexible formatting options.

Math.floor() and Math.ceil() methods:

• Uses Math.floor() to round down and Math.ceil() to round up.

BigDecimal class:

• Employs the BigDecimal class for precision control during rounding.

Choose the method that best suits your rounding requirements, considering factors like precision, direction (up or down), and ease of use.

## Example 1: Edge Cases of Math.round() Method

1. Math.round of a NAN number will return 0. For example,

Implementation

• Java

### Java

``class Solution {    public static void main(String[] args) {        System.out.println("Welcome to Coding Ninjas Studio Online Compiler!\n");    	        System.out.println("2.0%0 is a "+ 2.0%0 +" number");    	        System.out.println("Math.round() of a NAN number return "+Math.round(2.0%0));    }}``

Output

2. The Math.round() will return Integer.MIN_VALUE for negative infinity or value less than or equal to Integer.MIN_VALUE.

Implementation

• Java

### Java

``class Solution {    public static void main(String[] args) {        System.out.println("Welcome to Coding Ninjas Studio Online Compiler!\n");    	        System.out.println("Integer.MIN_VALUE "+ Integer.MIN_VALUE);        System.out.println("Math.round() "+ Math.round(Integer.MIN_VALUE));    }}``

Output

3. The Math.round() will return Integer.MAX_VALUE for positive infinity or value greater than or equal to Integer.MAX_VALUE.

Implementation

• Java

### Java

``class Solution {    public static void main(String[] args) {        System.out.println("Welcome to Coding Ninjas Studio Online Compiler!\n");    	        System.out.println("Integer.MAX_VALUE "+ Integer.MAX_VALUE);        System.out.println("Math.round() "+ Math.round(Integer.MAX_VALUE));    }}``

Output

## Example 2: Java Math.round() with double

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));   }}``

Output

Must Read Conditional Statements in Java.

## Frequently Asked Questions

### What is the use of Math.ceil() method?

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.