From df943f471e5d18a1b5265a7745fbf5a0540b5bc1 Mon Sep 17 00:00:00 2001 From: Yang Jun Date: Wed, 8 Apr 2026 00:44:50 +0800 Subject: [PATCH] fix: round negative half away from zero to match Ruby/Shopify behavior Math.round(-2.5) returns -2 in JS (toward zero), but Ruby rounds to -3 (away from zero). Use sign-preserving abs+round to match Shopify Liquid. Closes #871 Made-with: Cursor --- src/filters/math.ts | 3 ++- test/integration/filters/math.spec.ts | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/filters/math.ts b/src/filters/math.ts index 219d7e64b..f746652cc 100644 --- a/src/filters/math.ts +++ b/src/filters/math.ts @@ -15,5 +15,6 @@ export function round (v: number, arg = 0) { v = toNumber(v) arg = toNumber(arg) const amp = Math.pow(10, arg) - return Math.round(v * amp) / amp + const n = v * amp + return (Math.sign(n) * Math.round(Math.abs(n))) / amp } diff --git a/test/integration/filters/math.spec.ts b/test/integration/filters/math.spec.ts index be77d3ec4..336aa7455 100644 --- a/test/integration/filters/math.spec.ts +++ b/test/integration/filters/math.spec.ts @@ -68,6 +68,8 @@ describe('filters/math', function () { it('should return "183.36" for 183.357,2', () => test('{{183.357|round: 2}}', '183.36')) it('should convert string to number', () => test('{{"2.7"|round}}', '3')) + it('should round -2.5 to -3 (away from zero)', () => test('{{ -2.5 | round }}', '-3')) + it('should round -1.5 to -2 (away from zero)', () => test('{{ -1.5 | round }}', '-2')) }) describe('times', function () { it('should return "6" for 3,2', () => test('{{ 3 | times: 2 }}', '6'))