Skip to content

Commit 1cdf10b

Browse files
authored
fix: rounding negative away from zero when half (#873)
1 parent 4f9a499 commit 1cdf10b

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

src/filters/math.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ export function round (v: number, arg = 0) {
1515
v = toNumber(v)
1616
arg = toNumber(arg)
1717
const amp = Math.pow(10, arg)
18-
return Math.round(v * amp) / amp
18+
const scaled = v * amp
19+
// Round half away from zero
20+
return Math.sign(v) * Math.round(Math.abs(scaled)) / amp
1921
}

test/integration/filters/math.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ describe('filters/math', function () {
6565
describe('round', function () {
6666
it('should return "1" for 1.2', () => test('{{1.2|round}}', '1'))
6767
it('should return "3" for 2.7', () => test('{{2.7|round}}', '3'))
68+
it('should return "-3" for -2.5 (away from zero)', () => test('{{num|round}}', { num: -2.5 }, '-3'))
69+
it('should return "-2" for -2.49 (closest integer)', () => test('{{num|round}}', { num: -2.49 }, '-2'))
6870
it('should return "183.36" for 183.357,2',
6971
() => test('{{183.357|round: 2}}', '183.36'))
7072
it('should convert string to number', () => test('{{"2.7"|round}}', '3'))

0 commit comments

Comments
 (0)