How To Use The Java Math.round() Method
Maybe your like
Key takeaways:
The Math.round() method rounds a number to the nearest integer by adding 0.5 and flooring the result.
To round numbers to a specific decimal place, multiply the number by 10n10^n, apply Math.round(), and then divide by the same factor.
Round up if the decimal part is 0.5 or greater, and round down if it's less than 0.5.
Math.round() handles edge cases like NaN and extreme integer values (e.g., Integer.MAX_VALUE).
Rounding is widely used in financial calculations, scientific data, and simplifying values in various domains.
Rounding GPS coordinates to a specific number of decimal places is helpful for presenting location data in a more human-readable format, particularly when working with map applications. For instance, a GPS coordinate of 40.74777740.747777 (latitude) might be rounded to 40.7540.75 for simplicity.
In this Answer, we will learn how to round off a number using the Java Math.round() method.
The Math.round() method
The Math.round() method in Java is used to round a number to its closest integer. This is done by adding 1/21/2 to the number, taking the floor of the result, and casting the result to an integer data type.
Syntax
The following is the syntax of Math.round() method:
public static long round(double a)//Orpublic static long round(float a)Here a is the floating-point number (or type double) to be rounded. It returns the closest long value to the argument. The return value is rounded to the nearest integer.
Some of the edge cases of the Math.round() method are:
- If the argument is NaN (not a number), then the function will return 00.
- If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, then the function returns Integer.MIN_VALUE.
- If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, then the function returns Integer.MAX_VALUE.
Code example to use the round() function
We take a number 74.65 and pass it to the round() function. The output will be a nearest integer 75.
import java.lang.Math; // Needed to use Math.round()class Program { public static void main( String args[] ) { double num1 = 74.65; System.out.println(Math.round(num1)); }}RunNegative numbers
We pass a negative number -4.3 to the method and it returns -4.
import java.lang.Math; // Needed to use Math.round()class Program { public static void main( String args[] ) { double num1 = -4.3; System.out.println(Math.round(num1)); }}RunSpecific decimal places
If we want to round a decimal number up to specific decimal places, in this case 2 decimal places we multiply it by 100.0 , pass it to the Math.round() method and then divide it by 100.0.
import java.lang.Math; // Needed to use Math.round()class Program { public static void main( String args[] ) { double num1 = -13.56934; System.out.println(Math.round(num1 * 100.0) / 100.0); // Round to two decimal places }}RunNote: To round a number to a specific decimal place, multiply it by 10n10^n, round it using Math.round(), and then divide by 10n10^n, again.
Kickstart your programming journey with "Learn Java." Master essential concepts like input/output methods, user-defined methods, and basic data types. Build sequential, selective, and iterative programs through engaging hands-on projects.
Conclusion
In conclusion, the Math.round() method in Java is a simple but powerful tool for rounding floating-point numbers to the nearest integer. It has various applications in financial calculations, scientific measurements, and user interfaces. By understanding its syntax and functionality, we can ensure that our program handles rounding efficiently and accurately.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
How to round off 1.5 to 2 in Java?
Use Math.round() to round 1.5 to the nearest integer:
long result = Math.round(1.5); // Output: 2How to round to 2 decimal places?
We can multiply by 100, round, then divide by 100:
double result = Math.round(3.14159 * 100.0) / 100.0; // Output: 3.14What is the rule to round?
The rule to round:
- If the decimal part is 0.5 or greater, round up.
- If the decimal part is less than 0.5, round down.
How to round off decimals in Java?
For general rounding to the nearest integer, use Math.round(). For specific decimal places, multiply, round, and divide:
double result = Math.round(number * 100) / 100.0; // Rounds to 2 decimal placesRelevant Answers
Explore Courses
Free Resources
Copyright ©2026 Educative, Inc. All rights reservedTag » How To Roundup In Java
-
How To Round Up In Java Code Example
-
Rounding Of Numbers In Java - Vertex Academy
-
Java Round Up Any Number - Math - Stack Overflow
-
How To Round Up In Java - Linux Hint
-
Round Up A Number In Java | Delft Stack
-
How To Round A Number To N Decimal Places In Java - Baeldung
-
Java Math Round() - Programiz
-
Java - Round() Method - Tutorialspoint
-
Java Math Round() Method With Example - GeeksforGeeks
-
Java Math.round() Method With Examples - Javatpoint
-
How To Round Up To 2 Decimal Places In Java? - Intellipaat Community
-
Java Math.round() - Scaler Topics
-
How Do I Round A Decimal To Nearest Integer In Java? - Quora
-
Java - How To Round Double / Float Value To 2 Decimal Places