修复浮点型转整数错误

This commit is contained in:
xx
2025-03-08 16:11:33 +08:00
parent 6a2f398250
commit cf42be756c

View File

@@ -64,8 +64,12 @@ std::optional<int64> module::globalfuncs::toint(const sol::object& obj)
sol::type t = obj.get_type();
switch (t) {
case sol::type::number:
// 直接将数字转换为 int
return obj.as<int64>();
double value = obj.as<double>();
// 检查小数部分是否为零
if (std::floor(value) == value) {
return static_cast<int64>(value);
}
return std::nullopt;
case sol::type::string: {
// 尝试将字符串转换为 int
std::string s = obj.as<std::string>();