The floor function is usually used in mathematical calculations and can round a floating point number down. There are implementations of the floor function in many programming languages.
Here are examples of how to use the floor function in some common programming languages:
Python:
import math x = 3.9 y = math.floor(x) print(y) # 输出:3
JavaScript:
var x = 3.9; var y = Math.floor(x); console.log(y); // 输出:3
Java:
double x = 3.9; double y = Math.floor(x); System.out.println(y); // 输出:3.0
C :
#include <cmath> // 或者 #include <math.h> double x = 3.9; double y = floor(x); std::cout << y << std::endl; // 输出:3.0
In these examples, we round down the value of variable x, then store the result in variable y, and print it out. It should be noted that due to the precision of floating point numbers, the results of the floor function may vary slightly.
The above is the detailed content of How to use floor function. For more information, please follow other related articles on the PHP Chinese website!