Home > Java > javaTutorial > Round floating point numbers down using java's Math.floor() function

Round floating point numbers down using java's Math.floor() function

PHPz
Release: 2023-07-26 13:54:48
Original
2921 people have browsed it

Use java's Math.floor() function to round floating-point numbers down

In daily programming, we often encounter situations where floating-point numbers need to be rounded down. Java provides a convenient function Math.floor() to implement this function. This article will introduce the usage of Math.floor() and provide some code examples to help readers understand better.

Math.floor() function is a static method of the Math class in Java. Its function is to round down a floating point number, that is, to remove the decimal part of the floating point number and keep only the integer part. . The return value type of this function is double, and the result is the numerical value after rounding down.

The following is the function definition of the Math.floor() function:

public static double floor(double a)
Copy after login

Among them, the parameter a is a floating point number that needs to be rounded down.

Next, we use several examples to demonstrate the usage of the Math.floor() function.

Example 1:

double number = 5.6;
double floorNumber = Math.floor(number);
System.out.println("向下取整后的结果为:" + floorNumber);
Copy after login

The running result is:

向下取整后的结果为:5.0
Copy after login

In the above example, we define a floating point number and pass it to Math.floor() The function rounds down. Finally, print the results. Since the decimal part of 5.6 is 0.6, the result after rounding down is 5.0.

Example 2:

double number = -3.9;
double floorNumber = Math.floor(number);
System.out.println("向下取整后的结果为:" + floorNumber);
Copy after login

The running result is:

向下取整后的结果为:-4.0
Copy after login

In the above example, we define a floating point number and pass it to Math.floor() The function rounds down. Since the decimal part of -3.9 is 0.9, the result after rounding down is -4.0.

Example 3:

double number = 10.0 / 3.0;
double floorNumber = Math.floor(number);
System.out.println("向下取整后的结果为:" + floorNumber);
Copy after login

The running result is:

向下取整后的结果为:3.0
Copy after login

In the above example, we first divide 10.0 by 3.0 to get a floating point number, and then pass it to The Math.floor() function rounds down. Since the result of 10.0/3.0 is 3.33333333..., the result after rounding down is 3.0.

It should be noted that the Math.floor() function treats integers the same way. It actually treats integer values ​​and floating-point values ​​equally. It only focuses on the decimal part and discards the part that is greater than the decimal part.

To sum up, using Java's Math.floor() function can conveniently round down floating point numbers and keep only the integer part. I hope that readers can better understand and master the usage of the Math.floor() function through the introduction and examples of this article, and can flexibly apply it in actual programming.

The above is the detailed content of Round floating point numbers down using java's Math.floor() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template