mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-02 21:32:10 +03:00
feat: add K/M/B/T cost shortener to prevent UI overflow (#1902)
Integrated into release/v3.7.9
This commit is contained in:
@@ -146,6 +146,55 @@ export function formatCost(usd: number | null | undefined): string {
|
||||
|
||||
export const fmtCost = formatCost;
|
||||
|
||||
/**
|
||||
* Format a USD cost for display using abbreviated K/M/B/T suffixes.
|
||||
* Sub-cent values show additional precision.
|
||||
* - Values >= 1T are shown as $X.XT
|
||||
* - Values >= 1B are shown as $X.XB
|
||||
* - Values >= 1M are shown as $X.XM
|
||||
* - Values >= 1K are shown as $X.XK
|
||||
* - Otherwise shown as $X.XX
|
||||
* @param {number} usd - Cost in USD
|
||||
* @returns {string}
|
||||
*/
|
||||
export function formatCostAbbreviated(usd: number | null | undefined): string {
|
||||
const value = Number(usd || 0);
|
||||
if (!Number.isFinite(value) || value === 0) return "$0";
|
||||
const abs = Math.abs(value);
|
||||
if (abs < 0.01) {
|
||||
if (value < 0) {
|
||||
return `-$${Math.abs(value).toFixed(6)}`;
|
||||
}
|
||||
return `$${value.toFixed(6)}`;
|
||||
}
|
||||
let divisor: number, suffix: string;
|
||||
if (abs >= 1_000_000_000_000) {
|
||||
divisor = 1_000_000_000_000;
|
||||
suffix = "T";
|
||||
} else if (abs >= 1_000_000_000) {
|
||||
divisor = 1_000_000_000;
|
||||
suffix = "B";
|
||||
} else if (abs >= 1_000_000) {
|
||||
divisor = 1_000_000;
|
||||
suffix = "M";
|
||||
} else if (abs >= 1_000) {
|
||||
divisor = 1_000;
|
||||
suffix = "K";
|
||||
} else {
|
||||
if (value < 0) {
|
||||
return `-$${Math.abs(value).toFixed(2)}`;
|
||||
}
|
||||
return `$${value.toFixed(2)}`;
|
||||
}
|
||||
const abbreviated = abs / divisor;
|
||||
let formatted = abbreviated.toFixed(1);
|
||||
if (formatted.includes(".")) {
|
||||
formatted = formatted.replace(/\.?0+$/, "");
|
||||
}
|
||||
const sign = value < 0 ? "-" : "";
|
||||
return `${sign}$${formatted}${suffix}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate a URL for compact display.
|
||||
* @param {string} url - Full URL
|
||||
|
||||
Reference in New Issue
Block a user