Skip to content

Commit

Permalink
format currency with thousand separators (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
justcallmelarry authored Feb 5, 2024
1 parent fb49fb5 commit be0964d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/app/groups/[groupId]/balances-list.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Balances } from '@/lib/balances'
import { cn } from '@/lib/utils'
import { cn, formatCurrency } from '@/lib/utils'
import { Participant } from '@prisma/client'

type Props = {
Expand Down Expand Up @@ -28,7 +28,7 @@ export function BalancesList({ balances, participants, currency }: Props) {
</div>
<div className={cn('w-1/2 relative', isLeft || 'text-right')}>
<div className="absolute inset-0 p-2 z-20">
{currency} {(balance / 100).toFixed(2)}
{formatCurrency(currency, balance)}
</div>
{balance !== 0 && (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import { ToastAction } from '@/components/ui/toast'
import { useToast } from '@/components/ui/use-toast'
import { useMediaQuery } from '@/lib/hooks'
import { formatExpenseDate } from '@/lib/utils'
import { formatCurrency, formatExpenseDate } from '@/lib/utils'
import { Category } from '@prisma/client'
import { ChevronRight, FileQuestion, Loader2, Receipt } from 'lucide-react'
import { getImageData, usePresignedUpload } from 'next-s3-upload'
Expand Down Expand Up @@ -185,9 +185,7 @@ export function CreateFromReceiptButton({
<div>
{receiptInfo ? (
receiptInfo.amount ? (
<>
{groupCurrency} {receiptInfo.amount.toFixed(2)}
</>
<>{formatCurrency(groupCurrency, receiptInfo.amount)}</>
) : (
<Unknown />
)
Expand Down
4 changes: 2 additions & 2 deletions src/app/groups/[groupId]/expenses/expense-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CategoryIcon } from '@/app/groups/[groupId]/expenses/category-icon'
import { Button } from '@/components/ui/button'
import { SearchBar } from '@/components/ui/search-bar'
import { getGroupExpenses } from '@/lib/api'
import { cn, formatExpenseDate } from '@/lib/utils'
import { cn, formatCurrency, formatExpenseDate } from '@/lib/utils'
import { Expense, Participant } from '@prisma/client'
import dayjs, { type Dayjs } from 'dayjs'
import { ChevronRight } from 'lucide-react'
Expand Down Expand Up @@ -156,7 +156,7 @@ export function ExpenseList({
expense.isReimbursement ? 'italic' : 'font-bold',
)}
>
{currency} {(expense.amount / 100).toFixed(2)}
{formatCurrency(currency, expense.amount)}
</div>
<div className="text-xs text-muted-foreground">
{formatExpenseDate(expense.expenseDate)}
Expand Down
5 changes: 2 additions & 3 deletions src/app/groups/[groupId]/reimbursement-list.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Button } from '@/components/ui/button'
import { Reimbursement } from '@/lib/balances'
import { formatCurrency } from '@/lib/utils'
import { Participant } from '@prisma/client'
import Link from 'next/link'

Expand Down Expand Up @@ -42,9 +43,7 @@ export function ReimbursementList({
</Link>
</Button>
</div>
<div>
{currency} {(reimbursement.amount / 100).toFixed(2)}
</div>
<div>{formatCurrency(currency, reimbursement.amount)}</div>
</div>
))}
</div>
Expand Down
9 changes: 9 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ export function formatExpenseDate(date: Date) {
export function formatCategoryForAIPrompt(category: Category) {
return `"${category.grouping}/${category.name}" (ID: ${category.id})`
}

export function formatCurrency(currency: string, amount: number) {
const format = new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})
const formattedAmount = format.format(amount / 100)
return `${currency} ${formattedAmount}`
}

0 comments on commit be0964d

Please sign in to comment.