Skip to content

Commit

Permalink
Voting: use @aragon/ui formatTokenAmount() rather than local formatAm…
Browse files Browse the repository at this point in the history
…ount()

- Also, remove formatAmount() from math-utils.js
  • Loading branch information
ECWireless committed Jun 18, 2020
1 parent e706887 commit 758cd8a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 41 deletions.
7 changes: 4 additions & 3 deletions apps/voting/app/src/components/SummaryRows.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react'
import styled from 'styled-components'
import { GU, textStyle, useTheme } from '@aragon/ui'
import { formatTokenAmount, GU, textStyle, useTheme } from '@aragon/ui'
import You from './You'
import { formatNumber } from '../math-utils'
import { VOTE_NAY, VOTE_YEA } from '../vote-types'

function SummaryRows({ yea, nay, symbol, connectedAccountVote }) {
Expand Down Expand Up @@ -69,7 +68,9 @@ function SummaryRow({ color, label, pct, token, youVoted }) {
margin-left: ${2 * GU}px;
`}
>
{formatNumber(token.amount, 5)} {token.symbol}
{formatTokenAmount(token.amount, 0, {
symbol: token.symbol
})}
</div>
</div>
)
Expand Down
38 changes: 0 additions & 38 deletions apps/voting/app/src/math-utils.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,5 @@
import BN from 'bn.js'

/**
* Format numbers for a given number of decimal places
*
* @param {number} num Number to round
* @param {number} [decimals=2] Number of decimals to round to
* @param {Object} [options] Options object
* @param {bool} [options.truncate=true] Whether to truncate the trailing decimals (if they're 0)
* @returns {String} Formatted number
*/
export function formatNumber(num, decimals = 2, { truncate = true } = {}) {
const multiplicator = Math.pow(10, decimals)
const roundedNum = Math.round(num * multiplicator) / multiplicator
const numString = String(roundedNum)

if (!decimals) {
return numString
}

const exponentialIndex = numString.indexOf('e+')
const numWithoutExponents =
exponentialIndex > -1 ? numString.substring(0, exponentialIndex) : numString

const [whole, decimal = ''] = numWithoutExponents.split('.')
const trimmedDecimals = truncate ? decimal.replace(/0+$/, '') : decimals
const formattedNumber = trimmedDecimals.length
? `${whole}.${
trimmedDecimals.length > decimals
? trimmedDecimals.slice(0, decimals)
: trimmedDecimals
}`
: whole

// If we were dealing with a yuge number, append the exponent suffix back
return exponentialIndex > -1
? `${formattedNumber}${numString.substring(exponentialIndex)}`
: formattedNumber
}

export function percentageList(values, digits = 0) {
return scaleBNValuesSet(values).map(value => value.toNumber())
}
Expand Down

0 comments on commit 758cd8a

Please sign in to comment.