Skip to content

fix: 修复 amis 表达式 CEIL/UPPERMONEY 浮点乘法导致的计算错误 Close: #21533 - #21535

Open
lhy-dcf wants to merge 1 commit into
baidu:masterfrom
lhy-dcf:fix/formula-ceil-uppermoney
Open

lhy-dcf wants to merge 1 commit into
baidu:masterfrom
lhy-dcf:fix/formula-ceil-uppermoney

Conversation

@lhy-dcf

@lhy-dcf lhy-dcf commented Nov 21, 2026

Copy link
Copy Markdown

问题

amis-formula 的 UPPERMONEY 与 CEIL 在做「乘 10 的幂 + 取整」时直接使用二进制浮点乘法的结果:

  • UPPERMONEY(19.99) 的分位由 Math.floor(19.99 * 10 * Math.pow(10, 1)) % 10 取得,19.99 * 100 = 1998.9999999999998,floor 后取到 8,输出「壹拾玖元玖角捌分」,期望「玖分」;同理 UPPERMONEY(1.13) → 「壹元壹角贰分」(期望「叁分」)、UPPERMONEY(0.57) → 「伍角陆分」(期望「柒分」)。
  • CEIL(1.1, 2) 由 Math.ceil(1.1 * 100) / 100 取得,1.1 * 100 = 110.00000000000001,ceil 后多进一位,返回 1.11,期望 1.1;同理 CEIL(0.07, 2) 返回 0.08(期望 0.07)。

#12232 / #12239 已为 FLOOR 修过同类问题(乘积先 toFixed(10) 消除尾差),本 PR 给 CEIL 与 UPPERMONEY 沿用同样的处理。

另外,角为零、分不为零时 UPPERMONEY 会把「零角」整体丢掉:UPPERMONEY(1.05) 返回「壹元伍分」、UPPERMONEY(100.05) 返回「壹佰元伍分」。按人民币大写规范应为「壹元零伍分」「壹佰元零伍分」。

修复

  • fnCEIL:与 fnFLOOR 一致,改为 Math.ceil(parseFloat((a * c).toFixed(10))) / c。
  • fnUPPERMONEY:
    • 角/分位取数同样先 toFixed(10) 消除浮点尾差;
    • 角为零、分不为零且存在整数部分时保留「零角」,交给既有的 (零.)+ 归一逻辑统一输出为「元零X分」。不影响 UPPERMONEY(0.05) → 伍分、UPPERMONEY(1.00) → 壹元整、UPPERMONEY(7682) → 柒仟陆佰捌拾贰元整 等原有行为。

测试

  • packages/amis-formula/__tests__/fomula.test.ts 与 __tests__/async-fomula.test.ts 各新增 8 条断言(issue 中的 7 个复现 + UPPERMONEY(100.05)),并把 UPPERMONEY(7682.01) 的期望从「柒仟陆佰捌拾贰元壹分」修正为「柒仟陆佰捌拾贰元零壹分」(角为零需补「零」)。
  • 修复前新断言为红(如 CEIL(1.1, 2) 返回 1.11、UPPERMONEY(19.99) 返回「…玖角捌分」);修复后 amis-formula 包全部 9 个测试套件、185 项测试通过。
  • tsc --noEmit 无新增错误。

Close: #21533

@github-actions github-actions Bot added the fix label Nov 21, 2026
@github-actions

Copy link
Copy Markdown

👍 Thanks for this!
🏷 I have applied any labels matching special text in your issue.

Please review the labels and make any necessary changes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

1 participant