Decimals: Fixes auto decimals to behave the same for positive and negative values (#53960)

pull/53969/head
Joao Silva 3 years ago committed by GitHub
parent b6835ef87d
commit 4f2d30b153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      packages/grafana-data/src/valueFormats/valueFormats.test.ts
  2. 5
      packages/grafana-data/src/valueFormats/valueFormats.ts

@ -94,6 +94,15 @@ describe('valueFormats', () => {
expect(toFixed(100.4)).toBe('100');
expect(toFixed(100.5)).toBe('101');
expect(toFixed(27.4)).toBe('27.4');
expect(toFixed(27.5)).toBe('27.5');
expect(toFixed(-100)).toBe('-100');
expect(toFixed(-100.5)).toBe('-100');
expect(toFixed(-100.6)).toBe('-101');
expect(toFixed(-27.5)).toBe('-27.5');
expect(toFixed(-27.6)).toBe('-27.6');
});
it('toFixed should handle number correctly if decimal is not null', () => {

@ -80,10 +80,11 @@ export function toFixed(value: number, decimals?: DecimalCount): string {
}
function getDecimalsForValue(value: number): number {
const log10 = Math.floor(Math.log(Math.abs(value)) / Math.LN10);
const absValue = Math.abs(value);
const log10 = Math.floor(Math.log(absValue) / Math.LN10);
let dec = -log10 + 1;
const magn = Math.pow(10, -dec);
const norm = value / magn; // norm is between 1.0 and 10.0
const norm = absValue / magn; // norm is between 1.0 and 10.0
// special case for 2.5, requires an extra decimal
if (norm > 2.25) {

Loading…
Cancel
Save