How to Use Math.round() in Java

Have you ever needed to round a number in your Java code? In Java, the Math.round() function is used to round a floating-point number to the nearest integer. This function is a built-in method in the Math class and can be used in various scenarios. In this article, we will discuss what the Math.round() method does, how it works, and some examples of how to use it in your Java code.

Table of Contents

What is the Math.round() method?

The Math.round() method is a built-in function in the Java Math class that rounds a floating-point number to the nearest integer. This method takes a single argument, which is the number to be rounded. The Math.round() method returns a long value, which is the rounded value of the given floating-point number.

How does the Math.round() method work?

The Math.round() method works by taking a floating-point number as input and then rounding it to the nearest integer. The method first checks whether the fractional part of the input number is greater than or equal to 0.5. If the fractional part is greater than or equal to 0.5, the method rounds the number up to the nearest integer. If the fractional part is less than 0.5, the method rounds the number down to the nearest integer.

Examples of using the Math.round() method

Let’s take a look at some examples of using the Math.round() method in Java code:

Example 1: Rounding a float to the nearest integer

float number = 2.7f;
long roundedNumber = Math.round(number);
System.out.println("Original number: " + number);
System.out.println("Rounded number: " + roundedNumber);

Output:

Original number: 2.7
Rounded number: 3

In this example, we have a floating-point number 2.7f. We use the Math.round() method to round this number to the nearest integer. The output shows that the rounded number is 3.

Example 2: Rounding a double to the nearest integer

double number = 5.4;
long roundedNumber = Math.round(number);
System.out.println("Original number: " + number);
System.out.println("Rounded number: " + roundedNumber);

Output:

Original number: 5.4
Rounded number: 5

In this example, we have a double-precision floating-point number 5.4. We use the Math.round() method to round this number to the nearest integer. The output shows that the rounded number is 5.

Example 3: Rounding a negative number to the nearest integer

float number = -3.8f;
long roundedNumber = Math.round(number);
System.out.println("Original number: " + number);
System.out.println("Rounded number: " + roundedNumber);

Output:

Original number: -3.8
Rounded number: -4

In this example, we have a negative floating-point number -3.8f. We use the Math.round() method to round this number to the nearest integer. The output shows that the rounded number is -4.

Example 4: Rounding a number to a specific number of decimal places

double number = 4.5678;
double roundedNumber = Math.round(number * 100.0) / 100.0;
System.out.println("Original number: " + number);
System.out.println("Rounded number: " + roundedNumber);

Output:

Original number: 4.5678
Rounded number: 4.57

In this example, we have a double-precision floating-point number 4.5678. We use the Math.round() method to round this number to two decimal places. To do this, we multiply the number by 100.0, round it using the Math.round() method, and then divide it by 100.0 to get the rounded number to two decimal places. The output shows that the rounded number is 4.57.

Conclusion

In conclusion, the Math.round() method is a useful function to have in your Java code when you need to round floating-point numbers to the nearest integer or to a specific number of decimal places. It’s easy to use and is a built-in function in the Math class. By using the Math.round() method, you can ensure that your code produces accurate and precise results.

Leave a Comment

Your email address will not be published. Required fields are marked *