diff --git a/src/filters/math.ts b/src/filters/math.ts index fa06b4ef8..cb6775bff 100644 --- a/src/filters/math.ts +++ b/src/filters/math.ts @@ -15,7 +15,6 @@ export function round (v: number, arg = 0) { v = toNumber(v) arg = toNumber(arg) const amp = Math.pow(10, arg) - const scaled = v * amp - // Round half away from zero - return Math.sign(v) * Math.round(Math.abs(scaled)) / amp + const scaled = (v * amp) * (1 + Number.EPSILON) + return Math.round(scaled) / amp } diff --git a/test/integration/filters/math.spec.ts b/test/integration/filters/math.spec.ts index e8a4722a5..ab52cb4be 100644 --- a/test/integration/filters/math.spec.ts +++ b/test/integration/filters/math.spec.ts @@ -70,6 +70,10 @@ 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('odd number 1.005 should round correctly', () => test('{{1.005|round:2}}', '1.01')) + it('odd number -1.005 should round correctly', () => test('{{num|round:2}}', { num: -1.005 }, '-1.01')) + it('odd number 9.075 should round correctly', () => test('{{9.075|round:2}}', '9.08')) + it('odd number -9.075 should round correctly', () => test('{{num|round:2}}', { num: -9.075 }, '-9.08')) }) describe('times', function () { it('should return "6" for 3,2', () => test('{{ 3 | times: 2 }}', '6'))