-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: consistent decimal points (#1088)
## Closes #1074 ### [ Summary ] - Unify formatting utilities into one file - Expand them to cover more cases - Make 2 decimals as default - Display full number of balance on hover over wallet card - Sticked to using `Intl.NumberFormat` api for localized formatting --------- Co-authored-by: Brian Pearce <[email protected]> Co-authored-by: brianp <[email protected]>
- Loading branch information
1 parent
a207f0f
commit 643d847
Showing
14 changed files
with
94 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import i18n from 'i18next'; | ||
|
||
export enum FormatPreset { | ||
PERCENT = 'percent', | ||
TXTM_COMPACT = 'txtm-compact', | ||
TXTM_LONG = 'txtm-crypto', | ||
DECIMAL_COMPACT = 'decimal-compact', | ||
} | ||
|
||
const removeDecimals = (value: number, decimals: number) => { | ||
return value / Math.pow(10, decimals); | ||
}; | ||
|
||
const removeTXTMCryptoDecimals = (value: number) => { | ||
return removeDecimals(value, 6); | ||
}; | ||
|
||
const formatValue = (value: number, options: Intl.NumberFormatOptions = {}): string => | ||
Intl.NumberFormat(i18n.language, options).format(value); | ||
|
||
const formatPercent = (value = 0) => formatValue(value, { style: 'percent', maximumFractionDigits: 2 }); | ||
|
||
const formatTXTMCompact = (value: number) => | ||
formatValue(removeTXTMCryptoDecimals(value), { | ||
minimumFractionDigits: 2, | ||
maximumFractionDigits: 2, | ||
notation: 'compact', | ||
style: 'decimal', | ||
}); | ||
|
||
const formatTXTMLong = (value: number) => | ||
formatValue(removeTXTMCryptoDecimals(value), { maximumFractionDigits: 2, notation: 'standard', style: 'decimal' }); | ||
|
||
const formatDecimalCompact = (value: number) => formatValue(value, { maximumFractionDigits: 2, style: 'decimal' }); | ||
|
||
export function formatNumber(value: number, preset: FormatPreset): string { | ||
switch (preset) { | ||
case FormatPreset.PERCENT: | ||
return formatPercent(value); | ||
case FormatPreset.TXTM_COMPACT: | ||
return formatTXTMCompact(value); | ||
case FormatPreset.TXTM_LONG: | ||
return formatTXTMLong(value); | ||
case FormatPreset.DECIMAL_COMPACT: | ||
return formatDecimalCompact(value); | ||
default: | ||
console.error('Unknown format preset:', preset); | ||
return '-'; | ||
} | ||
} | ||
|
||
export function formatHashrate(hashrate: number, joinUnit = true): string { | ||
if (hashrate < 1000) { | ||
return joinUnit ? hashrate + ' H/s' : hashrate.toFixed(2); | ||
} else if (hashrate < 1000000) { | ||
return (hashrate / 1000).toFixed(2) + (joinUnit ? ' kH/s' : 'k'); | ||
} else if (hashrate < 1000000000) { | ||
return (hashrate / 1000000).toFixed(2) + (joinUnit ? ' MH/s' : 'M'); | ||
} else if (hashrate < 1000000000000) { | ||
return (hashrate / 1000000000).toFixed(2) + (joinUnit ? ' GH/s' : 'G'); | ||
} else if (hashrate < 1000000000000000) { | ||
return (hashrate / 1000000000000).toFixed(2) + (joinUnit ? ' TH/s' : 'T'); | ||
} else { | ||
return (hashrate / 1000000000000000).toFixed(2) + (joinUnit ? ' PH/s' : 'P'); | ||
} | ||
} |