-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correct numbers in voting results
- Loading branch information
Showing
4 changed files
with
67 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const toPrecise3 = new Intl.NumberFormat('en-US', { | ||
maximumSignificantDigits: 3, | ||
}); | ||
function removeTrailingZeros(value: string): string { | ||
return value.replace(/\.?0+$/, ''); | ||
} | ||
|
||
export function amountToShortString(num: number): string { | ||
if (num < 1) { | ||
return toPrecise3.format(num); | ||
} | ||
if (num >= 1 && num < 1e3) { | ||
return removeTrailingZeros(num.toFixed(2)); | ||
} | ||
if (num >= 1e3 && num < 1e6) { | ||
return removeTrailingZeros((num / 1e3).toFixed(1)) + 'K'; | ||
} | ||
if (num >= 1e6 && num < 1e9) { | ||
return removeTrailingZeros((num / 1e6).toFixed(1)) + 'M'; | ||
} | ||
if (num >= 1e9 && num < 1e12) { | ||
return removeTrailingZeros((num / 1e9).toFixed(1)) + 'B'; | ||
} | ||
if (num >= 1e12) { | ||
return removeTrailingZeros((num / 1e12).toFixed(1)) + 'T'; | ||
} | ||
return num.toFixed(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters