floor

floor(int|float $num): float

Rounds down toward negative infinity: floor(-2.5) is -3 while an int cast truncates toward zero giving -2 - the negative-direction trap is the core teaching point paired with ceil.

C C23

C23
#include <math.h>
#include <stdio.h>

int main(void) {
    // floor rounds toward -infinity; the int cast truncates toward zero
    printf("%.0f\n", floor(4.7));
    printf("%.0f\n", floor(-2.5)); // -3, not -2
    printf("%d\n", (int)(-2.5));   // truncation: -2
    return 0;
}
Output
4
-3
-2

C++ C++26

C++26
#include <cmath>
#include <iostream>

int main() {
    std::cout << std::floor(4.7) << "\n";
    std::cout << std::floor(-2.5) << "\n";          // -3, not -2
    std::cout << static_cast<int>(-2.5) << "\n";    // truncation: -2
}
Output
4
-3
-2

C# v11.0.100

v11.0.100
// Math.Floor rounds toward -infinity; a cast truncates toward zero
Console.WriteLine(Math.Floor(4.7));
Console.WriteLine(Math.Floor(-2.5));  // -3, not -2
Console.WriteLine((int)(-2.5));       // truncation: -2
Output
4
-3
-2

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Floor(4.7))
	fmt.Println(math.Floor(-2.5)) // -3, not -2
	x := -2.5
	fmt.Println(int(x)) // truncation: -2
}
Output
4
-3
-2

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // printing Math.floor's double would show 4.0 - cast to long to keep the output clean
        System.out.println((long) Math.floor(4.7));
        System.out.println((long) Math.floor(-2.5)); // -3, not -2
        System.out.println((int) (-2.5));            // truncation: -2
    }
}
Output
4
-3
-2

JavaScript v26.13.1

v26.13.1
// Math.floor toward -infinity; Math.trunc toward zero
console.log(Math.floor(4.7));
console.log(Math.floor(-2.5));  // -3, not -2
console.log(Math.trunc(-2.5));  // truncation: -2
Output
4
-3
-2

PHP v8.6.0

v8.6.0
// floor rounds toward -infinity; an int cast rounds toward ZERO - they differ on negatives
echo floor(4.7), "\n";
echo floor(-2.5), "\n";   // -3, not -2
echo (int)(-2.5), "\n";   // truncation: -2
Output
4
-3
-2

Python v3.15.2

v3.15.2
# math.floor toward -infinity (returns int); int() truncates toward zero
import math

print(math.floor(4.7))
print(math.floor(-2.5))  # -3, not -2
print(int(-2.5))         # truncation: -2
Output
4
-3
-2

Rust v1.99.1

v1.99.1
fn main() {
    // f64::floor toward -infinity; the as-cast truncates toward zero
    println!("{}", 4.7f64.floor());
    println!("{}", (-2.5f64).floor()); // -3, not -2
    println!("{}", -2.5f64 as i32);    // truncation: -2
}
Output
4
-3
-2

TypeScript v26.13.1

v26.13.1
// Math.floor toward -infinity; Math.trunc toward zero
console.log(Math.floor(4.7));
console.log(Math.floor(-2.5));  // -3, not -2
console.log(Math.trunc(-2.5));  // truncation: -2
Output
4
-3
-2

Language differences

C: floor(3) lives in <math.h>; it always returns double - print with %.0f to drop the decimal point.

C++: std::floor.

C#: Math.Floor returns double.

Go: math.Floor returns float64.

Java: Math.floor returns double and prints with .0 - the sample casts to long.

JavaScript: Math.floor; keep Math.trunc in mind for bucketing near negatives.

PHP: floor returns float (the value is already integral); differing from the (int) cast on negatives is the core of this entry; pair it with the ceil entry: ceil for pagination, floor for bucket alignment.

Python: math.floor returns int directly (most others return a float type).

Rust: f64::floor; as i32 is truncation.

TypeScript: same as JavaScript.