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
This commit is contained in:
Yang Jun
2026-04-08 00:44:50 +08:00
parent 4f9a49988a
commit df943f471e
2 changed files with 4 additions and 1 deletions
+2 -1
View File
@@ -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
}
+2
View File
@@ -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'))