Wiki

effect · round_variable

Definition

  • Supported scope:any
  • Supported target:none

Description

Rounds a variable
Example: round_variable = num_dogs

实战 · 配合 · 坑

实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。

实战用法

round_variable 常用于需要将经过复杂数学运算(乘除、百分比换算等)后产生小数的变量整理为整数的场景,例如计算某国的工厂产出分成、人口配额或资源分配比例后对结果取整,避免后续比较或显示时出现浮点误差。

# 例:根据工业值的 30% 计算奖励,取整后再使用
set_variable = { var = reward_amount value = industrial_capacity_factor }
multiply_variable = { var = reward_amount value = 0.3 }
round_variable = reward_amount
# 此后 reward_amount 已是整数,可安全用于比较或赋值

配合关系

  • [multiply_variable](/wiki/effect/multiply_variable) — 乘法运算后的结果极易产生小数,通常紧接着用 round_variable 对结果取整。
  • [divide_variable](/wiki/effect/divide_variable) — 除法几乎必然产生浮点数,是最频繁需要配合取整的前置操作。
  • [clamp_variable](/wiki/effect/clamp_variable) — 取整之后常常还需要将变量限制在合理区间内,两者搭配保证数值合法。
  • [check_variable](/wiki/trigger/check_variable) — 取整后才去做变量比较判断,能避免因浮点精度差异导致条件永远无法精确命中的问题。

常见坑

  1. 对临时变量用错命令round_variable 只作用于持久变量(variable),若要对 temp_variable 取整必须改用 round_temp_variable,否则脚本不会报错但也不会有任何效果,极难排查。
  2. 误以为取整是向下截断:HOI4 的 round_variable 执行的是四舍五入而非直接向下取整(floor),若业务逻辑需要严格的向下取整,应先用 modulo_variable 减去余数再操作,不能直接依赖 round_variable 替代地板除。

Hands-On Notes

Hands-on notes are AI-generated and checked against the vanilla command vocabulary — treat them as a starting point, not authoritative reference. The definition above is the game's own documentation.

Hands-On Usage

round_variable is commonly used in scenarios where a variable needs to be converted to an integer after undergoing complex mathematical operations (multiplication, division, percentage calculations, etc.). For example, when calculating factory output shares, population quotas, or resource allocation ratios for a country, rounding the result to an integer prevents floating-point errors in subsequent comparisons or displays.

# Example: Calculate reward based on 30% of industrial capacity, round to integer, then use
set_variable = { var = reward_amount value = industrial_capacity_factor }
multiply_variable = { var = reward_amount value = 0.3 }
round_variable = reward_amount
# After this, reward_amount is guaranteed to be an integer and can be safely used in comparisons or assignments

Synergy

  • [multiply_variable](/wiki/effect/multiply_variable) — Multiplication operations very easily produce decimals; typically round_variable immediately follows to round the result.
  • [divide_variable](/wiki/effect/divide_variable) — Division almost inevitably produces floating-point numbers and is the most frequent operation requiring subsequent rounding.
  • [clamp_variable](/wiki/effect/clamp_variable) — After rounding, variables often need to be constrained within a reasonable range; using both together ensures numerical validity.
  • [check_variable](/wiki/trigger/check_variable) — Perform variable comparisons only after rounding to avoid conditions that can never be precisely met due to floating-point precision discrepancies.

Common Pitfalls

  1. Using the wrong command on temporary variables: round_variable only operates on persistent variables (variable). To round a temp_variable, you must use round_temp_variable instead. The script will not throw an error, but the operation will have no effect, making it extremely difficult to debug.
  2. Mistaking rounding for floor division: HOI4's round_variable performs standard rounding (banker's rounding) rather than direct truncation. If your logic requires strict floor division, use modulo_variable to subtract the remainder first; do not rely on round_variable as a substitute for floor operations.