top

Search

Java Tutorial

.

UpGrad

Java Tutorial

Math Floor in Java

Introduction

In Java, there are several functions to work on mathematical calculations. These are predefined functions that accept value and return results. One such handy mathematical function is the Math Floor Java which allows you to round a given decimal number to its nearest whole number or integer, similar to the Math.ceil Java method. However, unlike Math.ceil(), the floor function provides the largest integer that’s lesser than or equal to the given value.

The Math.floor() method is quite simple. Here, one needs to just give a desired decimal number as an input to the method, and the function will simply round it down to its nearest integer value. It can be applied to each element of an array.

Math Floor can be applied wherever one needs precise control over numerical operations, such as financial calculations and scientific research.

Overview

Math.floor() is a useful tool for solving various mathematical operations, especially when discarding a fractional part of a decimal value and working with whole numbers only. It is also noteworthy how this function works with negative numbers to ensure consistent rounding behaviors. 

Java Math.floor() Method

The floor() method in the Math class in Java is a built-in mathematical function for rounding up a decimal or floating-point number down to the nearest integer or whole number. 

Syntax and Parameters of Math.floor() in Java

Here is the syntax of the Math.floor() method in Java:

public static double floor(double d)

Let us break down the syntax:

  • public: It specifies that the method can be accessed from any other class.

  • static: It indicates that the method belongs to the Math class rather than an instance of the class. You can directly call the method using the class name Math.

  • double: It indicates that the method returns a value of type double. In this case, the rounded-down value of the input d will be returned.

  • floor: It is the name of the method that represents the action of rounding down.

  • (double d): It specifies the parameter that the method expects, which is a type double value denoted by the identifier d. This is the value that will be rounded down.

Return Values of Math floor() in Java

The Math.floor() method in Java returns a value of type double, the largest integer that is less than or equal to the specified value. The returned value is computed by rounding the specified value towards negative infinity.

For example, if we have a value d of 3.8, the floor() method will return 3.0 because 3 is the largest integer less than or equal to 3.8. Similarly, if we have a value d of -2.5, the method will return -3.0 because -3 is the largest integer less than or equal to -2.5.

double value = 3.8;
double result = Math.floor(value);
System.out.println(result); // Output: 3.0

Exception of floor() in Java

The floor() method in Java does not throw any exceptions. It is a well-defined method in the Math class and is designed to handle the rounding down operation without any exceptional scenarios.

Therefore, when you use the floor() method, you don't need to handle any specific exceptions in your code. It will always return a result without throwing any exceptions.

However, it's important to note that if you pass NaN (Not-a-Number) or positive infinity as the argument to the floor() method, the result will be NaN or positive infinity, respectively. These are not exceptions but specific cases defined by the IEEE 754 floating-point standard.

Examples

Example 1: Math.floor() in Java

public class upGradTutorials {
    public static void main(String[] args) {
        double num = 7.8;
        double result = Math.floor(num);
        System.out.println("Original number: " + num);
        System.out.println("Floored number: " + result);
    }
}

In the above example, we have a variable num initialized with the value 7.8. We use the Math.floor() method to round down the value of num to the nearest integer or whole number that is less than or equal to it. The result is stored in the variable result.

Finally, we print both the original and floored numbers to the console using System.out.println().

Example 2: Using Java Math floor() function with Array

import java.util.Arrays;

public class upGradTutorials {
    public static void main(String[] args) {
        // Define an array of decimal numbers
        double[] numbers = {2.5, 6.7, 4.1, 9.8, 3.3};
        
        // Print the original array
        System.out.println("Original array: " + Arrays.toString(numbers));

        // Iterate through each element of the array and round it down using Math.floor()
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = Math.floor(numbers[i]);
        }

        // Print the modified array with floored values
        System.out.println("Floored array: " + Arrays.toString(numbers));
    }
}

In the above program, we have an array called numbers that contains decimal values. We want to round down each value in the array to the nearest integer or whole number.

First, we print the original array using the Arrays.toString() method. Then, we use a loop to iterate through each array element. Within the loop, we apply the Math.floor() method to round down each value to the nearest integer. The modified value is then returned to the same index in the array.

Finally, we print the modified array using the Arrays.toString() method to display the floored values.

Example 3: Using Java Math.floor() function with ArrayList

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class upGradTutorials {
    public static void main(String[] args) {
        // Create an ArrayList of decimal numbers
        List<Double> numbers = new ArrayList<>(Arrays.asList(3.5, 9.1, 6.2, 4.7));

        // Print the original ArrayList
        System.out.println("Original ArrayList: " + numbers);

        // Iterate through each element of the ArrayList and round it down using Math.floor()
        for (int i = 0; i < numbers.size(); i++) {
            double roundedDown = Math.floor(numbers.get(i));
            numbers.set(i, roundedDown);
        }

        // Print the modified ArrayList with floored values
        System.out.println("Floored ArrayList: " + numbers);
    }
}

In this example, we create an ArrayList called numbers containing decimal values using the Arrays.asList() method. We want to round down each value in the ArrayList to the nearest integer.

First, we print the original ArrayList using the System.out.println() statement.

Then, we iterate through each element of the ArrayList using a loop. Within the loop, we use the Math.floor() method to round down each value to the nearest integer. The rounded-down value is then returned to the ArrayList using the set() method.

Finally, we print the modified ArrayList containing the floored values using the System.out.println() statement.

Example 4: To show the working of floor() with a positive double value

public class upGradTutorials {
    public static void main(String[] args) {
        double num = 7.8;

        double floored = Math.floor(num);

        System.out.println("Original number: " + num);
        System.out.println("Floored number: " + floored);
    }
}

In this example, we have a variable num initialized with the value 7.8. The Math.floor() method is used to round down the value of num to the nearest integer or whole number that is less than or equal to it. The rounded-down value is stored in the variable floored.

Finally, we print both the original and floored numbers to the console.

Example 5: To show the working of floor() with a negative double value

public class upGradTutorials {
    public static void main(String[] args) {
        double num = -5.3;

        double floored = Math.floor(num);

        System.out.println("Original number: " + num);
        System.out.println("Floored number: " + floored);
    }
}

First, we declare a variable num and assign it the value -5.3, representing a negative decimal number. Next, we call the Math.floor() method and pass num as the argument. This method returns the largest integer value that is less than or equal to the given number.

The result of the Math.floor() method is stored in the variable floored. Finally, we use System.out.println() statements to display the original number (num) and the floored number (floored) on the console.

Conclusion

From what we learned from this tutorial, it is clear how the Math.floor() function can perform precise calculations in a code. Thus, making it a must-learn function for all the programmers out there. 

There are many other uses for the Math.floor(), such as its application in converting the Java floor double to int just by casting the result. This simplicity in its application, coupled with the accurate results it delivers, makes this function an essential part of a programmer's toolkit.

Thus, with the understanding of Math.floor() one can draw out the most precise and accurate calculations while solving the difficult programming challenges.

FAQs

  1. What is the Math floor in Java?

Math floor in Java is a mathematical function in the programming language that rounds down a given decimal number to the nearest integer. To be precise, it returns the largest integer less than or equal to the given number while discarding the decimal part. 

  1. What is the Math floor (-4.7) in Java?

As we know, the Math.floor() function rounds the given number to its nearest integer, which will be less than or equal to the number. Therefore, in this case, the result of the Math.floor(-4.7) is -5 .

  1. How to apply floor in Java?

To apply the floor function in Java, one can simply use the Math.floor() method. One must pass their desired decimal number as an argument to the function to apply it. In return, it will give out the result by rounding it down to the largest integer that is less than or equal to the given number.

  1. What is Math.floor JavaScript?

Math.floor is a JavaScript function that rounds a number down to the nearest integer, returning the largest integer less than or equal to the given number.

Leave a Reply

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