app/vmui: do not round the number in formatPrettyNumber() if the range isn't set

This commit is contained in:
Aliaksandr Valialkin 2023-01-17 19:50:37 -08:00
parent 006af394ff
commit c94020f7dc
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 13 additions and 2 deletions

View file

@ -40,7 +40,7 @@ const Index: FC = () => {
const getQueryStatsTitle = (key: keyof TopQueryStats) => {
if (!data) return key;
const value = data[key];
if (typeof value === "number") return formatPrettyNumber(value, 0, value);
if (typeof value === "number") return formatPrettyNumber(value);
return value || key;
};

View file

@ -37,7 +37,18 @@ export const formatPrettyNumber = (n: number | null | undefined, min = 0, max =
if (n === undefined || n === null) {
return "";
}
let digits = 3 + Math.floor(1 + Math.log10(Math.max(Math.abs(min), Math.abs(max))) - Math.log10(Math.abs(min - max)));
const range = Math.abs(max - min);
if (isNaN(range) || range == 0) {
// Return the constant number as is if the range isn't set of it is too small.
if (Math.abs(n) >= 1000) {
return n.toLocaleString("en-US");
}
return n.toString();
}
// Make sure n has 3 significant digits on the given range.
// This precision should be enough for most UX cases,
// since the remaining digits are usually a white noise.
let digits = 3 + Math.floor(1 + Math.log10(Math.max(Math.abs(min), Math.abs(max))) - Math.log10(range));
if (isNaN(digits) || digits > 20) {
digits = 20;
}