fix: rounding negative away from zero when half (#873)

This commit is contained in:
Timmy Braun
2026-04-08 00:52:35 +08:00
committed by GitHub
parent 4f9a49988a
commit 1cdf10b57d
2 changed files with 5 additions and 1 deletions
+3 -1
View File
@@ -15,5 +15,7 @@ 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 scaled = v * amp
// Round half away from zero
return Math.sign(v) * Math.round(Math.abs(scaled)) / amp
} }
+2
View File
@@ -65,6 +65,8 @@ describe('filters/math', function () {
describe('round', function () { describe('round', function () {
it('should return "1" for 1.2', () => test('{{1.2|round}}', '1')) 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.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', 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'))