Friday, January 17, 2025
HomeProgrammingJava Math.round() method with Examples

Java Math.round() method with Examples

The Math.round() method in Java is used to round a floating-point number (either float or double) to the nearest integer. It returns the result as a long value for double inputs and an int value for float inputs.

Syntax:

public static long round(double a);
public static int round(float a);
  • round(double a): Rounds a double value to the nearest integer. The result is returned as a long.
  • round(float a): Rounds a float value to the nearest integer. The result is returned as an int.

Rounding Logic:

  • If the fractional part of the number is less than 0.5, it rounds down (towards zero).
  • If the fractional part of the number is greater than or equal to 0.5, it rounds up (away from zero).

Examples:

Example 1: Rounding a double value

public class RoundExample {
    public static void main(String[] args) {
        double value1 = 4.3;
        double value2 = 4.7;
        double value3 = -4.3;
        double value4 = -4.7;

        System.out.println("Rounded value of 4.3: " + Math.round(value1));  // Output: 4
        System.out.println("Rounded value of 4.7: " + Math.round(value2));  // Output: 5
        System.out.println("Rounded value of -4.3: " + Math.round(value3)); // Output: -4
        System.out.println("Rounded value of -4.7: " + Math.round(value4)); // Output: -5
    }
}

Explanation:

  • 4.3 rounds down to 4 because the fractional part (0.3) is less than 0.5.
  • 4.7 rounds up to 5 because the fractional part (0.7) is greater than or equal to 0.5.
  • Similarly, -4.3 rounds to -4 (towards zero), and -4.7 rounds to -5 (away from zero).
See also  Why is the volatile keyword used in C?

Example 2: Rounding a float value

public class RoundFloatExample {
    public static void main(String[] args) {
        float value1 = 5.4f;
        float value2 = 5.6f;
        float value3 = -5.4f;
        float value4 = -5.6f;

        System.out.println("Rounded value of 5.4: " + Math.round(value1));  // Output: 5
        System.out.println("Rounded value of 5.6: " + Math.round(value2));  // Output: 6
        System.out.println("Rounded value of -5.4: " + Math.round(value3)); // Output: -5
        System.out.println("Rounded value of -5.6: " + Math.round(value4)); // Output: -6
    }
}

Explanation:

  • The rounding behavior for float values is the same as for double. The method rounds 5.4 to 5 and 5.6 to 6. The same logic applies to negative values.
See also  How Do You Do Block Comments in YAML?

Example 3: Rounding a double with larger numbers

public class RoundLargeNumberExample {
    public static void main(String[] args) {
        double largeValue1 = 1234567.89;
        double largeValue2 = 1234567.51;

        System.out.println("Rounded value of 1234567.89: " + Math.round(largeValue1));  // Output: 1234568
        System.out.println("Rounded value of 1234567.51: " + Math.round(largeValue2));  // Output: 1234568
    }
}

Explanation:

  • 1234567.89 rounds up to 1234568 because the fractional part .89 is greater than 0.5.
  • 1234567.51 rounds up to 1234568 as well, since .51 is greater than or equal to 0.5.

Example 4: Using Math.round() in a real-world context

public class RoundSalaryExample {
    public static void main(String[] args) {
        double salary = 12345.67;
        System.out.println("Salary after rounding: " + Math.round(salary));  // Output: 12346
    }
}

Explanation:

  • A salary value of 12345.67 is rounded to 12346, as .67 is greater than 0.5.
See also  What is the Difference Between SCSS and SASS

Important Notes:

  1. Rounding a positive number: If the fractional part is .5 or greater, it will round up. Otherwise, it rounds down.
  2. Rounding a negative number: For negative values, the number rounds away from zero if the fractional part is .5 or greater, and towards zero if it’s less than .5.
  3. Return Type: Math.round() for float returns an int, while for double, it returns a long.

Summary:

  • The Math.round() method is useful when you need to round a floating-point number to the nearest integer.
  • The method rounds up if the decimal part is >= 0.5, and rounds down if it’s < 0.5.
  • It returns long for double values and int for float values.
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x