How to round down in Java?
In Java, you can use the Math.floor() function to round down.
The specific methods are as follows:
- Call the Math.floor() function.
- Pass the number that needs to be rounded down as the argument to the Math.floor() function.
- The result returned by the function is the value rounded down.
Here is an example code:
double number = 3.7;
double result = Math.floor(number); // 向下取整,结果为3.0
System.out.println(result);
Note: The data type returned by the Math.floor() function is double, if you need to convert the result to an integer, you can use type casting.
The sample code is as follows:
double number = 3.7;
int result = (int) Math.floor(number); // 向下取整并转换为整型,结果为3
System.out.println(result);