Skip to content

Commit

Permalink
feat: thousand separator
Browse files Browse the repository at this point in the history
  • Loading branch information
honeymaro committed Nov 28, 2022
1 parent 50b3e94 commit ecf8074
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 @@ -104,13 +104,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 ecf8074

Please sign in to comment.