Linux

How to round of Numbers in Java

Java provides a built-in class known as Math class which belongs to the java.lang package. The java.lang.Math class provides numerous methods that are used to perform different numeric operations such as rounding of a number, finding square root, and so on. The Math class offers multiple methods to round off a number such as round(), ceil(), floor().

This article will provide a profound knowledge about how to round of numbers in java. In this write-up we will cover the below listed method to round of a number in java:

  • What is Math.round() and how to use it in Java
  • What is Math.ceil() and how to use it in Java
  • What is Math.floor() and how to use it in Java

So, let’s get started!

What is Math.round() and how to use it in Java

It is a predefined method of Java’s Math class that cuts off the floating value and returns the closest integer-type number.

Example
This example will assist you to understand how to use the Math.round() method in java:

public class RoundingOfNumber {
    public static void main(String[] args) {
        float number1 = 172.52f;
        double number2 = 172.12;
        float number3 = 10.12f;
        float number4 = 10.72f;
        double number5 = 570.82;
       
        System.out.println(“Rounding of 172.52: “ + Math.round(number1));
        System.out.println(“Rounding of 172.12: “ + Math.round(number2));
        System.out.println(“Rounding of 10.12: “ + Math.round(number3));
        System.out.println(“Rounding of 10.72: “ + Math.round(number4));
        System.out.println(“Rounding of -570.82: “ + Math.round(number5));
    }
}

Above program will produce the following output:

The output shows that the Math.round() method returned the closest integer number.

What is Math.ceil() and how to use it in Java

It is a predefined method of Java’s Math class that rounds the given number upward and returns the double-type value; the below-given example will provide you more clarity on this concept.

Example
We will modify the previous example and instead of Math.round() method we will utilize the Math.ceil() method:

public class RoundingOfNumber {
    public static void main(String[] args) {
        float number1 = 172.52f;
        double number2 = 172.12;
        float number3 = 10.12f;
        float number4 = 10.72f;
        double number5 = 570.82;
       
        System.out.println(“Rounding of 172.52: “ + Math.ceil(number1));
        System.out.println(“Rounding of 172.12: “ + Math.ceil(number2));
        System.out.println(“Rounding of 10.12: “ + Math.ceil(number3));
        System.out.println(“Rounding of 10.72: “ + Math.ceil(number4));
        System.out.println(“Rounding of -570.82: “ + Math.ceil(number5));
    }
}

On successful execution of the program, we will get the following output:

The output shows that the Math.ceil() method returned upward nearest double-type values.

What is Math.floor() and how to use it in Java

This method opposes the concept of Math.ceil() method i.e. it rounds the given number downward and returns the double-type value.

Example
Let’s consider the below-given program to understand the working of the Math.floor() method.

public class RoundingOfNumber {
    public static void main(String[] args) {
        float number1 = 172.52f;
        double number2 = 172.12;
        float number3 = 10.12f;
        float number4 = 10.72f;
        double number5 = 570.82;
       
        System.out.println(“Rounding of 172.52: “ + Math.floor(number1));
        System.out.println(“Rounding of 172.12: “ + Math.floor(number2));
        System.out.println(“Rounding of 10.12: “ + Math.floor(number3));
        System.out.println(“Rounding of 10.72: “ + Math.floor(number4));
        System.out.println(“Rounding of -570.82: “ + Math.floor(number5));
    }
}

On successful execution of the above given code, we will get the following output:

The output shows that the Math.floor() method returned the downward nearest double-type values.

Conclusion

The Math class provides three methods to round off a number i.e. round(), ceil(), floor(). The math.round() method cuts off the floating value and returns the closest integer-type number. The ceil() method rounds the given number upward and returns the double-type value while the floor() method rounds the given number downward and returns the double-type value. This write-up explained the working of three built-in methods to round off a number in java i.e. round(), ceil(), and floor().

Source link

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button