Skip to content

Commit f2cf30f

Browse files
fix: pick the average unit from the rounded value
Turning off lowPrecision made numbro choose the unit before rounding, so 999,700 rendered as "1000k" instead of "1m". Choose the unit from the value rounded at the configured mantissa and pass it as forceAverage.
1 parent 93a2bd1 commit f2cf30f

4 files changed

Lines changed: 92 additions & 13 deletions

File tree

‎packages/app/src/__tests__/utils.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,20 @@ describe('formatNumber', () => {
517517
};
518518
expect(formatNumber(750, format)).toBe('750.0');
519519
expect(formatNumber(1500, format)).toBe('1.5k');
520+
expect(formatNumber(600000, format)).toBe('600.0k');
521+
});
522+
523+
it('moves to the next unit when rounding reaches 1000 of the current one', () => {
524+
const format: NumberFormat = { output: 'number', average: true };
525+
expect(formatNumber(999700, format)).toBe('1m');
526+
expect(formatNumber(-999700, format)).toBe('-1m');
527+
expect(formatNumber(999.6e6, format)).toBe('1b');
528+
expect(formatNumber(999.6, format)).toBe('1k');
529+
expect(formatNumber(999960, { ...format, mantissa: 1 })).toBe('1.0m');
530+
expect(formatNumber(999940, { ...format, mantissa: 1 })).toBe('999.9k');
531+
expect(formatNumber(9.9999, { output: 'percent', average: true })).toBe(
532+
'1k%',
533+
);
520534
});
521535

522536
it('still formats byte output when average is set', () => {

‎packages/app/src/utils.ts‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,32 @@ const formatAutoScaleData = (
10011001
return `${scaledValue.toFixed(mantissa)} ${units[i]}${rateSuffix}`;
10021002
};
10031003

1004+
const AVERAGE_UNITS = [
1005+
[1e3, 'thousand'],
1006+
[1e6, 'million'],
1007+
[1e9, 'billion'],
1008+
[1e12, 'trillion'],
1009+
] as const;
1010+
1011+
// numbro's default lowPrecision abbreviates anything that rounds to the next
1012+
// unit (750 -> "0.8k"), while turning it off picks the unit before rounding
1013+
// (999,700 -> "1000k"). Pick the unit from the rounded value instead.
1014+
function averageUnitOptions(
1015+
displayed: number,
1016+
mantissa: number,
1017+
): Pick<numbro.Format, 'forceAverage' | 'lowPrecision'> {
1018+
const abs = Math.abs(displayed);
1019+
const round = (v: number) => Math.round(v * 10 ** mantissa) / 10 ** mantissa;
1020+
let scaled = abs;
1021+
let unit: numbro.Format['forceAverage'];
1022+
for (const [power, name] of AVERAGE_UNITS) {
1023+
if (round(scaled) < 1000) break;
1024+
unit = name;
1025+
scaled = abs / power;
1026+
}
1027+
return unit ? { forceAverage: unit } : { lowPrecision: false };
1028+
}
1029+
10041030
export const formatNumber = (
10051031
value?: string | number,
10061032
options?: NumberFormat,
@@ -1057,15 +1083,20 @@ export const formatNumber = (
10571083
return formatDurationMs(ms);
10581084
}
10591085

1086+
// Factor is only currently available for the time output
1087+
const factor = options.output === 'time' ? (options.factor ?? 1) : 1;
1088+
10601089
const numbroFormat: numbro.Format = {
10611090
output: options.output || 'number',
10621091
mantissa: mantissa,
10631092
thousandSeparated: options.thousandSeparated || false,
10641093
average: options.average || false,
1065-
// numbro's default lowPrecision abbreviates anything >= 500 (750 -> "0.8k").
1066-
// numbro rejects lowPrecision without average, which byte forces off below.
10671094
...(options.average &&
1068-
options.output !== 'byte' && { lowPrecision: false }),
1095+
options.output !== 'byte' &&
1096+
averageUnitOptions(
1097+
value * factor * (options.output === 'percent' ? 100 : 1),
1098+
mantissa,
1099+
)),
10691100
...(options.output === 'byte' && {
10701101
base: options.decimalBytes ? 'decimal' : 'general',
10711102
spaceSeparated: true,
@@ -1076,9 +1107,6 @@ export const formatNumber = (
10761107
}),
10771108
};
10781109

1079-
// Factor is only currently available for the time output
1080-
const factor = options.output === 'time' ? (options.factor ?? 1) : 1;
1081-
10821110
return (
10831111
numbro(value * factor).format(numbroFormat) +
10841112
(options.unit ? ` ${options.unit}` : '')

‎packages/cli/src/__tests__/formatNumber.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ import { describe, expect, it } from '@jest/globals';
22

33
import { NumericUnit } from '@hyperdx/common-utils/dist/types';
44

5-
import { axisTickFormatter } from '@/shared/formatNumber';
5+
import { axisTickFormatter, formatNumber } from '@/shared/formatNumber';
6+
7+
describe('formatNumber average', () => {
8+
it('moves to the next unit when rounding reaches 1000 of the current one', () => {
9+
const format = { output: 'number' as const, average: true };
10+
expect(formatNumber(750, format)).toBe('750');
11+
expect(formatNumber(999700, format)).toBe('1m');
12+
expect(formatNumber(999.6e6, format)).toBe('1b');
13+
});
14+
});
615

716
// Mirrors packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts's
817
// formatAxisTick suite - this file is a documented @source port of that

‎packages/cli/src/shared/formatNumber.ts‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,32 @@ const formatAutoScaleData = (
165165
return `${scaledValue.toFixed(mantissa)} ${units[i]}${rateSuffix}`;
166166
};
167167

168+
const AVERAGE_UNITS = [
169+
[1e3, 'thousand'],
170+
[1e6, 'million'],
171+
[1e9, 'billion'],
172+
[1e12, 'trillion'],
173+
] as const;
174+
175+
// numbro's default lowPrecision abbreviates anything that rounds to the next
176+
// unit (750 -> "0.8k"), while turning it off picks the unit before rounding
177+
// (999,700 -> "1000k"). Pick the unit from the rounded value instead.
178+
function averageUnitOptions(
179+
displayed: number,
180+
mantissa: number,
181+
): Pick<numbro.Format, 'forceAverage' | 'lowPrecision'> {
182+
const abs = Math.abs(displayed);
183+
const round = (v: number) => Math.round(v * 10 ** mantissa) / 10 ** mantissa;
184+
let scaled = abs;
185+
let unit: numbro.Format['forceAverage'];
186+
for (const [power, name] of AVERAGE_UNITS) {
187+
if (round(scaled) < 1000) break;
188+
unit = name;
189+
scaled = abs / power;
190+
}
191+
return unit ? { forceAverage: unit } : { lowPrecision: false };
192+
}
193+
168194
export const formatNumber = (
169195
value?: string | number,
170196
options?: NumberFormat,
@@ -221,15 +247,20 @@ export const formatNumber = (
221247
return formatDurationMs(ms);
222248
}
223249

250+
// Factor is only currently available for the time output
251+
const factor = options.output === 'time' ? (options.factor ?? 1) : 1;
252+
224253
const numbroFormat: numbro.Format = {
225254
output: options.output || 'number',
226255
mantissa: mantissa,
227256
thousandSeparated: options.thousandSeparated || false,
228257
average: options.average || false,
229-
// numbro's default lowPrecision abbreviates anything >= 500 (750 -> "0.8k").
230-
// numbro rejects lowPrecision without average, which byte forces off below.
231258
...(options.average &&
232-
options.output !== 'byte' && { lowPrecision: false }),
259+
options.output !== 'byte' &&
260+
averageUnitOptions(
261+
value * factor * (options.output === 'percent' ? 100 : 1),
262+
mantissa,
263+
)),
233264
...(options.output === 'byte' && {
234265
base: options.decimalBytes ? 'decimal' : 'general',
235266
spaceSeparated: true,
@@ -240,9 +271,6 @@ export const formatNumber = (
240271
}),
241272
};
242273

243-
// Factor is only currently available for the time output
244-
const factor = options.output === 'time' ? (options.factor ?? 1) : 1;
245-
246274
return (
247275
numbro(value * factor).format(numbroFormat) +
248276
(options.unit ? ` ${options.unit}` : '')

0 commit comments

Comments
 (0)