Java Math round() Method

The Math.round() method in Java is used to return the closest integer or long to the argument, with ties rounding to positive infinity.

Table of Contents

  1. Introduction
  2. round() Method Syntax
  3. Overloaded round() Methods
  4. Examples
    • round(double a)
    • round(float a)
  5. Real-World Use Case
  6. Conclusion

Introduction

The Math.round() method provides a way to round floating-point numbers (double and float) to their nearest integer representation. The method rounds halfway cases (values exactly between two integers) towards positive infinity.

round() Method Syntax

The syntax for the round() method varies depending on the types of the arguments:

round(double a)

public static long round(double a)

round(float a)

public static int round(float a)

Parameters:

  • a: The value to be rounded.

Returns:

  • For double: The closest long to the argument.
  • For float: The closest int to the argument.

Overloaded round() Methods

The Math.round() method is overloaded to handle different primitive data types: double and float. Each version returns the closest integer representation of the specified value, with ties rounding to positive infinity.

Examples

round(double a)

The round(double a) method returns the closest long to the specified double value.

Example

public class RoundDoubleExample {
    public static void main(String[] args) {
        double value1 = 3.5;
        double value2 = 2.3;
        double value3 = -2.5;
        double value4 = 4.5;

        long result1 = Math.round(value1);
        long result2 = Math.round(value2);
        long result3 = Math.round(value3);
        long result4 = Math.round(value4);

        System.out.println("Round " + value1 + " to " + result1);
        System.out.println("Round " + value2 + " to " + result2);
        System.out.println("Round " + value3 + " to " + result3);
        System.out.println("Round " + value4 + " to " + result4);
    }
}

Output:

Round 3.5 to 4
Round 2.3 to 2
Round -2.5 to -2
Round 4.5 to 5

round(float a)

The round(float a) method returns the closest int to the specified float value.

Example

public class RoundFloatExample {
    public static void main(String[] args) {
        float value1 = 3.5f;
        float value2 = 2.3f;
        float value3 = -2.5f;
        float value4 = 4.5f;

        int result1 = Math.round(value1);
        int result2 = Math.round(value2);
        int result3 = Math.round(value3);
        int result4 = Math.round(value4);

        System.out.println("Round " + value1 + " to " + result1);
        System.out.println("Round " + value2 + " to " + result2);
        System.out.println("Round " + value3 + " to " + result3);
        System.out.println("Round " + value4 + " to " + result4);
    }
}

Output:

Round 3.5 to 4
Round 2.3 to 2
Round -2.5 to -2
Round 4.5 to 5

Real-World Use Case

Rounding Prices

In real-world scenarios, the Math.round() method can be used to round prices to the nearest whole number, which is common in financial applications where prices need to be displayed without decimal places.

Example

public class RoundPricesExample {
    public static void main(String[] args) {
        double price = 9.99;
        double discount = 1.15;
        double finalPrice = price - discount;

        long roundedFinalPrice = Math.round(finalPrice);

        System.out.println("The final price after discount is " + finalPrice);
        System.out.println("The rounded final price is " + roundedFinalPrice);
    }
}

Output:

The final price after discount is 8.84
The rounded final price is 9

Conclusion

The Math.round() method in Java provides a way to round floating-point numbers to their nearest integer representation, with ties rounding to positive infinity. 

By understanding how to use this method and its overloaded versions, you can perform various rounding operations and solve problems involving rounding in your Java applications. 

Whether you are working with double or float values, the round() method offers a reliable tool for determining the rounded value.

Comments