Skip to content

Commit

Permalink
Merge pull request #148 from terraswap/feat/dashboard-format-number
Browse files Browse the repository at this point in the history
[classic] feat: thousand separator
  • Loading branch information
JoowonYun authored Dec 8, 2022
2 parents 584c259 + d2a2dec commit 6a55aea
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/libs/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,18 @@ export const toAmount = (value: string, contract_addr?: string) => {
return value ? new BigNumber(value).times(e).integerValue().toString() : "0"
}

export const formatNumber = (num: number | string) => {
const numberFormatter = Intl.NumberFormat("en-US")
return numberFormatter.format(Number(num))
}

export const formatMoney = (num: number, fix = 2) => {
const units = ["M", "B", "T", "Q"]
const unit = Math.floor((num / 1.0e1).toFixed(0).toString().length)
const r = unit % 3
const x =
Math.abs(Number(num)) / Number(Number("1.0e+" + (unit - r)).toFixed(2))
return units[Math.floor(unit / 3) - 2]
? x.toFixed(fix) + units[Math.floor(unit / 3) - 2]
: num.toFixed(fix)
? formatNumber(x.toFixed(fix)) + units[Math.floor(unit / 3) - 2]
: formatNumber(num.toFixed(fix))
}

0 comments on commit 6a55aea

Please sign in to comment.