Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DAO-945 Will show 0 when USD is NaN #493

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading