mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Decimals: Fixes auto decimals to behave the same for positive and negative values (#53960)
This commit is contained in:
parent
b6835ef87d
commit
4f2d30b153
@ -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…
Reference in New Issue
Block a user