various: eliminate floating-point noise before displaying

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ic14f4c0a9e0bcbe3460bbecc670f713d6a6a6964
This commit is contained in:
raf 2026-04-14 14:59:46 +03:00
commit d26a70dacd
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
3 changed files with 16 additions and 6 deletions

View file

@ -6,10 +6,11 @@ export function formatBytes(bytes: number, precision = 2): string {
}
export function formatNumber(num: number, precision = 2): string {
if (num >= 1e9) return (num / 1e9).toFixed(precision) + 'B';
if (num >= 1e6) return (num / 1e6).toFixed(precision) + 'M';
if (num >= 1e3) return (num / 1e3).toFixed(precision) + 'K';
return num.toString();
if (num >= 1e9) return parseFloat((num / 1e9).toFixed(precision)).toString() + 'B';
if (num >= 1e6) return parseFloat((num / 1e6).toFixed(precision)).toString() + 'M';
if (num >= 1e3) return parseFloat((num / 1e3).toFixed(precision)).toString() + 'K';
const factor = Math.pow(10, precision);
return (Math.round(num * factor) / factor).toString();
}
export function formatTime(seconds: number, precision = 2): string {

View file

@ -303,7 +303,13 @@ const ComparisonView: Component<ComparisonViewProps> = props => {
<Show when={!row.isReduction}>
<ArrowUpIcon size={14} />
</Show>
{Math.abs(row.change).toFixed(prec())}%
{parseFloat(
(
Math.round(Math.abs(row.change) * Math.pow(10, prec())) /
Math.pow(10, prec())
).toFixed(prec()),
)}
%
</span>
</Show>
}

View file

@ -49,7 +49,10 @@ const ThunkChart: Component<ThunkChartProps> = props => {
<div class="ratio-bar">
<div class="ratio-fill" style={{ width: `${avoidedRatio() * 100}%` }} />
</div>
<span class="ratio-label">Avoidance rate: {(avoidedRatio() * 100).toFixed(prec())}%</span>
<span class="ratio-label">
Avoidance rate:{' '}
{Math.round(avoidedRatio() * 100 * Math.pow(10, prec())) / Math.pow(10, prec())}%
</span>
</div>
</div>
);