ceil
2026/4/14大约 5 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — ceil
ceil
定义
ceil() 是一个向上取整的数学函数。所谓"向上取整",就是不管小数部分是多少——哪怕只有一丁点儿——都往更大的方向进一位。ceil 这个名字来自"天花板"(ceiling)——永远只往高处走。
打个比方:想象你去超市买塑料袋装苹果,每个袋子最多装 6 个。如果你有 13 个苹果,13 / 6 = 2.16... 个袋子,但现实中你不可能用 0.16 个袋子,所以你得买 3 个袋子才够装。ceil(2.16) 返回 3.0,就是这个道理。
再比如:你跑了 5.1 公里,跑步 app 说"你至少跑了 6 公里才完成今天的任务"。ceil(5.1) 返回 6.0——宁可多算,不能少算。
函数签名
C#
// Mathf.Ceil 返回 float 类型
public static float Ceil(float s)GDScript
func ceil(x: float) -> float参数说明
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
| x / s | float | 是 | 需要向上取整的浮点数值 |
返回值
类型: float
返回大于或等于 x 的最小整数,但类型仍然是浮点数(比如 4.0 而不是 4)。
| 输入值 | 返回值 | 说明 |
|---|---|---|
3.1 | 4.0 | 小数部分不为零,向上进一位 |
3.9 | 4.0 | 小数部分不为零,向上进一位 |
3.0 | 3.0 | 已经是整数,原样返回 |
-2.1 | -2.0 | 负数向上取整,向正方向靠近(-2 > -2.1) |
-2.0 | -2.0 | 已经是整数,原样返回 |
代码示例
基础用法
C#
using Godot;
public partial class CeilExample : Node
{
public override void _Ready()
{
// 正数向上取整
GD.Print(Mathf.Ceil(3.1f)); // 运行结果: 4
GD.Print(Mathf.Ceil(3.9f)); // 运行结果: 4
GD.Print(Mathf.Ceil(3.0f)); // 运行结果: 3
// 负数向上取整(注意方向)
GD.Print(Mathf.Ceil(-2.1f)); // 运行结果: -2
GD.Print(Mathf.Ceil(-2.9f)); // 运行结果: -2
}
}GDScript
func _ready():
# 正数向上取整
print(ceil(3.1)) # 运行结果: 4.0
print(ceil(3.9)) # 运行结果: 4.0
print(ceil(3.0)) # 运行结果: 3.0
# 负数向上取整(注意方向)
print(ceil(-2.1)) # 运行结果: -2.0
print(ceil(-2.9)) # 运行结果: -2.0实际场景:计算背包需要多少行
C#
using Godot;
public partial class InventoryGrid : Node
{
// 每行最多显示的物品数
[Export] public int ExItemsPerRow = 4;
// 计算总共需要多少行才能放下所有物品
private int CalculateRowCount(int totalItems)
{
return (int)Mathf.Ceil((float)totalItems / ExItemsPerRow);
}
public override void _Ready()
{
int totalItems = 15;
int rows = CalculateRowCount(totalItems);
GD.Print($"共 {totalItems} 个物品,每行 {ExItemsPerRow} 个,需要 {rows} 行");
// 运行结果: 共 15 个物品,每行 4 个,需要 4 行
int total2 = 12;
int rows2 = CalculateRowCount(total2);
GD.Print($"共 {total2} 个物品,每行 {ExItemsPerRow} 个,需要 {rows2} 行");
// 运行结果: 共 12 个物品,每行 4 个,需要 3 行
}
}GDScript
extends Node
# 每行最多显示的物品数
@export var ex_items_per_row: int = 4
# 计算总共需要多少行才能放下所有物品
func calculate_row_count(total_items: int) -> int:
return int(ceil(float(total_items) / ex_items_per_row))
func _ready():
var total_items := 15
var rows := calculate_row_count(total_items)
print("共 %d 个物品,每行 %d 个,需要 %d 行" % [total_items, ex_items_per_row, rows])
# 运行结果: 共 15 个物品,每行 4 个,需要 4 行
var total2 := 12
var rows2 := calculate_row_count(total2)
print("共 %d 个物品,每行 %d 个,需要 %d 行" % [total2, ex_items_per_row, rows2])
# 运行结果: 共 12 个物品,每行 4 个,需要 3 行进阶用法:计算分页总页数与技能点分配
C#
using Godot;
public partial class PaginationSystem : Node
{
// 计算分页的总页数(最后不满一页也算一页)
// 比如 47 条数据,每页 10 条,需要 5 页
private int GetTotalPages(int totalRecords, int perPage)
{
return (int)Mathf.Ceil((float)totalRecords / perPage);
}
public override void _Ready()
{
int records = 47;
int perPage = 10;
int pages = GetTotalPages(records, perPage);
GD.Print($"共 {records} 条记录,每页 {perPage} 条,总共 {pages} 页");
// 运行结果: 共 47 条记录,每页 10 条,总共 5 页
// 技能点分配:升级需要 3.2 个技能点,但技能点只能整数分配
// 所以必须凑够 4 个技能点才能升级
float requiredPoints = 3.2f;
int actualPoints = (int)Mathf.Ceil(requiredPoints);
GD.Print($"需要 {requiredPoints} 个技能点,实际需要准备 {actualPoints} 个");
// 运行结果: 需要 3.2 个技能点,实际需要准备 4 个
}
}GDScript
extends Node
# 计算分页的总页数(最后不满一页也算一页)
# 比如 47 条数据,每页 10 条,需要 5 页
func get_total_pages(total_records: int, per_page: int) -> int:
return int(ceil(float(total_records) / per_page))
func _ready():
var records := 47
var per_page := 10
var pages := get_total_pages(records, per_page)
print("共 %d 条记录,每页 %d 条,总共 %d 页" % [records, per_page, pages])
# 运行结果: 共 47 条记录,每页 10 条,总共 5 页
# 技能点分配:升级需要 3.2 个技能点,但技能点只能整数分配
# 所以必须凑够 4 个技能点才能升级
var required_points := 3.2
var actual_points := int(ceil(required_points))
print("需要 %s 个技能点,实际需要准备 %d 个" % [required_points, actual_points])
# 运行结果: 需要 3.2 个技能点,实际需要准备 4 个注意事项
返回值是 float,不是 int:
ceil()返回浮点数(如4.0),如果你需要整数类型,在 C# 中用(int)Mathf.Ceil(x)或Mathf.CeilToInt(x);在 GDScript 中用int(ceil(x))。负数的行为可能出乎意料:
ceil(-2.1)返回-2.0而不是-3.0,因为"向上"指的是数学上向正无穷方向(即数值更大的方向)。这和很多人直觉里的"远离零"不一样。与
floor()相反:floor()是地板函数(向下取整),ceil()是天花板函数(向上取整)。与
round()的区别:ceil()无条件向上,而round()是四舍五入——会根据小数部分的大小选择最近的整数。
