mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-20 15:00:42 -07:00
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:
+2
-1
@@ -15,5 +15,6 @@ export function round (v: number, arg = 0) {
|
|||||||
v = toNumber(v)
|
v = toNumber(v)
|
||||||
arg = toNumber(arg)
|
arg = toNumber(arg)
|
||||||
const amp = Math.pow(10, 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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,8 @@ describe('filters/math', function () {
|
|||||||
it('should return "183.36" for 183.357,2',
|
it('should return "183.36" for 183.357,2',
|
||||||
() => test('{{183.357|round: 2}}', '183.36'))
|
() => test('{{183.357|round: 2}}', '183.36'))
|
||||||
it('should convert string to number', () => test('{{"2.7"|round}}', '3'))
|
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 () {
|
describe('times', function () {
|
||||||
it('should return "6" for 3,2', () => test('{{ 3 | times: 2 }}', '6'))
|
it('should return "6" for 3,2', () => test('{{ 3 | times: 2 }}', '6'))
|
||||||
|
|||||||
Reference in New Issue
Block a user