fix(traffic): сохранять точность объёмов и обозначать бинарные единицы

This commit is contained in:
Denozordec committed 2026-10-12 21:59:50 +07:00
1 parent 2358ecef60
commit 2b3d583e3b
29 files changed
+1452 -39

No files matched your search

+11 -4
View File
@@ -10,10 +10,17 @@ export function fmtRate(v: number | null | undefined): string {
return `${Math.round(v)} Мбит/с`
}
export function fmtGB(v: number | null | undefined): string {
if (v == null) return "—"
if (v >= 1000) return `${(v / 1000).toFixed(2)} ТБ`
return `${v.toFixed(1)} ГБ`
const binaryFormatter = new Intl.NumberFormat("ru-RU", { maximumFractionDigits: 2 })
/** Traffic API volumeUnit=GiB: preserve binary units and never format positive bytes as zero. */
export function fmtGiB(v: number | null | undefined): string {
if (v == null || !Number.isFinite(v) || v < 0) return "—"
const bytes = v * 1024 ** 3
if (!Number.isFinite(bytes)) return "—"
if (bytes === 0) return "0 Б"
if (bytes < 1) return "<1 Б"
const units = ["Б", "КиБ", "МиБ", "ГиБ", "ТиБ", "ПиБ"]
const index = Math.min(units.length - 1, Math.floor(Math.log(bytes) / Math.log(1024)))
return `${binaryFormatter.format(bytes / 1024 ** index)} ${units[index]}`
}
export function formatBytes(n: number): string {