Format percentages consistently (#6955)

This commit is contained in:
Jaco 2022-02-09 12:29:14 +01:00 committed by GitHub
parent 335de54ea0
commit 8481fbfe8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,6 +35,10 @@ interface VoteType {
hasVotedAye: boolean;
}
function percentage (val: BN, div: BN): string {
return Math.min(100, val.muln(10000).div(div).toNumber() / 100).toFixed(2);
}
function Referendum ({ className = '', value: { allAye, allNay, image, imageHash, index, isPassing, status, voteCountAye, voteCountNay, votedAye, votedNay, votedTotal } }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
@ -58,11 +62,11 @@ function Referendum ({ className = '', value: { allAye, allNay, image, imageHash
{
aye: votedTotal.isZero()
? ''
: `${(aye.muln(10000).div(votedTotal).toNumber() / 100).toFixed(2)}%`,
: `${percentage(aye, votedTotal)}%`,
nay: votedTotal.isZero()
? ''
: `${(nay.muln(10000).div(votedTotal).toNumber() / 100).toFixed(2)}%`,
turnout: `${((votedTotal.muln(10000).div(totalIssuance).toNumber()) / 100).toFixed(2)}%`
: `${percentage(nay, votedTotal)}%`,
turnout: `${percentage(votedTotal, totalIssuance)}%`
},
{
hasVoted: hasVotedAye || allNay.some(({ accountId }) => allAccounts.includes(accountId.toString())),