diff --git a/src/ui/numeralFormat.ts b/src/ui/numeralFormat.ts index 3c7d27b0d..a93d54595 100644 --- a/src/ui/numeralFormat.ts +++ b/src/ui/numeralFormat.ts @@ -203,12 +203,16 @@ class NumeralFormatter { } furthestFrom0(n1: number, n2 = 0, n3 = 0): number { - const minValue = Math.min(n1, n2, n3); - if(minValue < 0) { - return minValue; - } else { - return Math.max(n1, n2, n3); + if(isNaN(n1)) n1=0; + if(isNaN(n2)) n2=0; + if(isNaN(n3)) n3=0; + const furthestAbsolute = Math.max(Math.abs(n1), Math.abs(n2), Math.abs(n3)); + switch(furthestAbsolute) { + case Math.abs(n1): return n1; + case Math.abs(n2): return n2; + case Math.abs(n3): return n3; } + return 0; } parseMoney(s: string): number { diff --git a/test/jest/ui/nFormat.test.js b/test/jest/ui/nFormat.test.js index 85460c707..e54d28323 100644 --- a/test/jest/ui/nFormat.test.js +++ b/test/jest/ui/nFormat.test.js @@ -218,3 +218,31 @@ describe('Numeral formatting of scientific text', () => { //expect(numeralWrapper.parseMoney('-123.456n')).toBeCloseTo(-123456000000000000000000000000000); }); }); + +describe('Finding the number furthest away from 0', () => { + test('should work if all numbers are equal', () => { + expect(numeralWrapper.furthestFrom0(0, 0, 0)).toEqual(0); + expect(numeralWrapper.furthestFrom0(1, 1, 1)).toEqual(1); + expect(numeralWrapper.furthestFrom0(123, 123, 123)).toEqual(123); + expect(numeralWrapper.furthestFrom0(-1, -1, -1)).toEqual(-1); + expect(numeralWrapper.furthestFrom0(-123, -123, -123)).toEqual(-123); + }); + test('should work for different positive numbers, and for the largest number in each spot', () => { + expect(numeralWrapper.furthestFrom0(1, 2, 3)).toEqual(3); + expect(numeralWrapper.furthestFrom0(456, 789, 123)).toEqual(789); + expect(numeralWrapper.furthestFrom0(789123, 123456, 456789)).toEqual(789123); + }); + test('should work for different negative numbers, and for the smallest number in each spot', () => { + expect(numeralWrapper.furthestFrom0(-1, -2, -3)).toEqual(-3); + expect(numeralWrapper.furthestFrom0(-456, -789, -123)).toEqual(-789); + expect(numeralWrapper.furthestFrom0(-789123, -123456, -456789)).toEqual(-789123); + }); + test('should work for combined positive and negative numbers', () => { + expect(numeralWrapper.furthestFrom0(1, -2, 3)).toEqual(3); + expect(numeralWrapper.furthestFrom0(-456, 789, -123)).toEqual(789); + expect(numeralWrapper.furthestFrom0(789123, -123456, -456789)).toEqual(789123); + }); + test('Should return 0 for invalid input', () => { + expect(numeralWrapper.furthestFrom0('abc', undefined, null)).toEqual(0); + }); +});