diff --git a/public/app/core/specs/ticks.test.ts b/public/app/core/specs/ticks.test.ts index 73d0e96cbd2..51713625745 100644 --- a/public/app/core/specs/ticks.test.ts +++ b/public/app/core/specs/ticks.test.ts @@ -22,4 +22,13 @@ describe('ticks', () => { expect(dec.scaledDecimals).toBe(3); }); }); + + describe('getStringPrecision()', () => { + it('"3.12" should return 2', () => { + expect(ticks.getStringPrecision('3.12')).toBe(2); + }); + it('"asd" should return 0', () => { + expect(ticks.getStringPrecision('asd.asd')).toBe(0); + }); + }); }); diff --git a/public/app/core/utils/ticks.ts b/public/app/core/utils/ticks.ts index 7efe6123a5e..81380269a99 100644 --- a/public/app/core/utils/ticks.ts +++ b/public/app/core/utils/ticks.ts @@ -201,6 +201,10 @@ export function getPrecision(num: number): number { * Get decimal precision of number stored as a string ("3.14" => 2) */ export function getStringPrecision(num: string): number { + if (isNaN((num as unknown) as number)) { + return 0; + } + const dotIndex = num.indexOf('.'); if (dotIndex === -1) { return 0;