Skip to content

Function floor()

📝 Description

The floor function returns the closest whole number that is less than or equal to the given value. It effectively "rounds down" by removing the decimal part.

Use Case: Perfect for determining the maximum number of full resources you can afford. If you have enough budget for 4.9 servers, floor will tell you that you can actually only provision 4.


💻 Syntax

floor(number)

💡 Examples

1. Basic Rounding Down:

# Result: 3
output "round_down" {
  value = floor(3.9)
}

2. Negative Numbers (Be Careful!):

In the negative scale, "less than" means moving further away from zero.

# Result: -3
output "round_negative" {
  value = floor(-2.1)
}

3. Budget Calculation (Real-world scenario):

If you have a budget of $100 and each instance costs $30:

locals {
  total_budget = 100
  instance_cost = 30
  max_instances = floor(local.total_budget / local.instance_cost) # 100 / 30 = 3.33 -> Result: 3
}

⚠️ Comparison Table

Input floor() (Down) ceil() (Up)
1.9 1 2
1.1 1 2
5.0 5 5
-2.1 -3 -2