Created
September 22, 2020 14:31
-
-
Save dmmarmol/cf619054b762e6589ec0d0ec5852a85d to your computer and use it in GitHub Desktop.
Format Numbers
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
// Currency | |
function useCurrencyFormat() { | |
const { code, currency } = useCountryContext(); | |
const options: Intl.NumberFormatOptions = { | |
style: 'currency', | |
currency, | |
minimumFractionDigits: 0, | |
maximumFractionDigits: 2, | |
}; | |
return (value?: number | null) => { | |
const fixed = truncateToDecimals(value ?? 0, 2); | |
const result = new Intl.NumberFormat(code, options).format(fixed); | |
return result; | |
}; | |
} |
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
// Percentage | |
function usePercentFormat() { | |
const { code } = useCountryContext(); | |
const options: Intl.NumberFormatOptions = { style: 'percent', minimumFractionDigits: 0, maximumFractionDigits: 2 }; | |
return (value?: number | null) => { | |
const fixed = truncateToDecimals(value ?? 0, 2); | |
const result = new Intl.NumberFormat(code, options).format(fixed / 100); | |
return result; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment