From 1cdf10b57d82f0592414efbfca19e204b37aea9f Mon Sep 17 00:00:00 2001 From: Timmy Braun <35117769+timbze@users.noreply.github.com> Date: Tue, 7 Apr 2026 10:52:35 -0600 Subject: [PATCH] fix: rounding negative away from zero when half (#873) --- src/filters/math.ts | 4 +++- test/integration/filters/math.spec.ts | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/filters/math.ts b/src/filters/math.ts index 219d7e64b..fa06b4ef8 100644 --- a/src/filters/math.ts +++ b/src/filters/math.ts @@ -15,5 +15,7 @@ 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 scaled = v * amp + // Round half away from zero + return Math.sign(v) * Math.round(Math.abs(scaled)) / amp } diff --git a/test/integration/filters/math.spec.ts b/test/integration/filters/math.spec.ts index be77d3ec4..e8a4722a5 100644 --- a/test/integration/filters/math.spec.ts +++ b/test/integration/filters/math.spec.ts @@ -65,6 +65,8 @@ describe('filters/math', function () { describe('round', function () { it('should return "1" for 1.2', () => test('{{1.2|round}}', '1')) it('should return "3" for 2.7', () => test('{{2.7|round}}', '3')) + it('should return "-3" for -2.5 (away from zero)', () => test('{{num|round}}', { num: -2.5 }, '-3')) + it('should return "-2" for -2.49 (closest integer)', () => test('{{num|round}}', { num: -2.49 }, '-2')) 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'))