Skip to content

Commit

Permalink
Merge pull request #493 from RootstockCollective/DAO-945
Browse files Browse the repository at this point in the history
DAO-945 Will show 0 when USD is NaN
  • Loading branch information
jessgusclark authored Dec 20, 2024
2 parents ea9bc86 + 4d2ed0b commit 0930bf3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/proposals/create/TreasuryWithdrawProposalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export const TreasuryWithdrawProposalForm = () => {
/>
</FormControl>
{amountValue?.toString() && (
<FormDescription>= USD {formatCurrency(amountUsd)}</FormDescription>
<FormDescription>= USD {formatCurrency(amountUsd, 'USD', 0)}</FormDescription>
)}
<FormMessage />
</FormItem>
Expand Down
11 changes: 8 additions & 3 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,28 @@ export const sanitizeInputNumber = (num: number) => {
* Formats a number as a currency
* @param value - The number to format
* @param currency - The currency to format the number as (default: 'USD')
* @param defaultIfNaN
* @returns The formatted currency string
* @example formatCurrency(123456.789) // '$123,456.79'
* @example formatCurrency(123456.789, 'EUR') // '€123,456.79'
* @example formatCurrency(0.0001) // '<$0.00'
* @example formatCurrency(0) // '$0.00'
*/
export const formatCurrency = (value: number, currency = 'USD'): string => {
export const formatCurrency = (value: number, currency = 'USD', defaultIfNaN?: number): string => {
if (0 < value && value < 0.01) {
return '<$0.01'
}

return new Intl.NumberFormat('en-US', {
const formattedValue = new Intl.NumberFormat('en-US', {
style: 'currency',
currency,
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(value)

if (defaultIfNaN !== undefined && Number.isNaN(value)) {
return defaultIfNaN.toString()
}
return formattedValue
}

/**
Expand Down

0 comments on commit 0930bf3

Please sign in to comment.