Skip to content

Commit

Permalink
Merge pull request #455 from consensusnetworks/hotfix/breakdown-metrics
Browse files Browse the repository at this point in the history
Fix undefined value in breakdown metrics
  • Loading branch information
ccali11 authored Nov 2, 2023
2 parents b638385 + 9baba64 commit 7832644
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions apps/web/src/composables/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@ export default function useFormat() {

function formatNumber(number: number) {
const SI_SYMBOL = ['', 'K', 'M', 'B', 'T', 'P', 'E']
const tier = Math.log10(Math.abs(number)) / 3 | 0
if(tier === 0) return number.toFixed(2)
const suffix = SI_SYMBOL[tier]
const scale = Math.pow(10, tier * 3)
const scaled = number / scale
return scaled.toFixed(2) + suffix
const tier = Math.floor(Math.log10(Math.abs(number)) / 3)
let scale, scaled, suffix

if (number === 0) {
return '0.00'
} else if (Math.abs(number) < 1) {
// Find the position of the first non-zero digit after the decimal point
const decimalPlaces = Math.ceil(-Math.log10(Math.abs(number)))
// Limit to 6 decimal places
const fixedDecimals = Math.min(decimalPlaces, 6)
return number.toFixed(fixedDecimals)
} else if (tier === 0) {
return number.toFixed(2)
} else {
suffix = SI_SYMBOL[tier]
scale = Math.pow(10, tier * 3)
scaled = number / scale
return scaled.toFixed(2) + suffix
}
}


function trimAndLowercaseAddress(address: string) {
return address.trim().toLowerCase()
Expand Down

0 comments on commit 7832644

Please sign in to comment.