Skip to content

Commit 1a231b3

Browse files
committed
feat: enhance ADA formatting with precision and suffixes for large values
1 parent 0e1c3b7 commit 1a231b3

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

tests/govtool-frontend/playwright/lib/helpers/adaFormat.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@ const LOVELACE = 1000000;
22

33
export const correctVoteAdaFormat = (
44
lovelace: number | undefined,
5-
locale: string | undefined = undefined
5+
precision = 2
66
) => {
77
if (lovelace) {
88
const ada = lovelace / LOVELACE;
9-
return ada.toLocaleString(locale, {
10-
maximumFractionDigits: 3,
11-
});
9+
if (ada < 1000)
10+
return ada.toLocaleString("en-us", {
11+
maximumFractionDigits: precision,
12+
});
13+
const suffixes = ["k", "M", "B", "T"];
14+
const divisors = [1000, 1000000, 1000000000, 1000000000000];
15+
for (let i = 0; i < suffixes.length; i++) {
16+
if (ada < divisors[i] * 1000) {
17+
return (ada / divisors[i]).toFixed(precision) + suffixes[i];
18+
}
19+
}
1220
}
1321
return "0";
1422
};

0 commit comments

Comments
 (0)