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'))