Skip to content

Commit 23d59bf

Browse files
committed
Merge remote-tracking branch 'base/main'
2 parents a3cdafa + 8742bd5 commit 23d59bf

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/app/groups/[groupId]/expenses/create-from-receipt-button.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export function CreateFromReceiptButton({
209209
groupCurrency,
210210
receiptInfo.amount,
211211
locale,
212+
true,
212213
)}
213214
</>
214215
) : (

src/lib/utils.test.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ describe('formatCurrency', () => {
5757
]
5858

5959
for (const variation of variations) {
60-
it(`formats ${variation.amount} in ${variation.locale}`, () => {
61-
expect(formatCurrency(currency, variation.amount, variation.locale)).toBe(
62-
variation.result,
63-
)
60+
it(`formats ${variation.amount} in ${variation.locale} without fractions`, () => {
61+
expect(
62+
formatCurrency(currency, variation.amount * 100, variation.locale),
63+
).toBe(variation.result)
64+
})
65+
it(`formats ${variation.amount} in ${variation.locale} with fractions`, () => {
66+
expect(
67+
formatCurrency(currency, variation.amount, variation.locale, true),
68+
).toBe(variation.result)
6469
})
6570
}
6671
})

src/lib/utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ export function formatCategoryForAIPrompt(category: Category) {
2828
return `"${category.grouping}/${category.name}" (ID: ${category.id})`
2929
}
3030

31+
/**
32+
* @param fractions Financial values in this app are generally processed in cents (or equivalent).
33+
* They are are therefore integer representations of the amount (e.g. 100 for USD 1.00).
34+
* Set this to `true` if you need to pass a value with decimal fractions instead (e.g. 1.00 for USD 1.00).
35+
*/
3136
export function formatCurrency(
3237
currency: string,
3338
amount: number,
3439
locale: string,
40+
fractions?: boolean,
3541
) {
3642
const format = new Intl.NumberFormat(locale, {
3743
minimumFractionDigits: 2,
@@ -40,7 +46,7 @@ export function formatCurrency(
4046
// '€' will be placed in correct position
4147
currency: 'EUR',
4248
})
43-
const formattedAmount = format.format(amount)
49+
const formattedAmount = format.format(fractions ? amount : amount / 100)
4450
return formattedAmount.replace('€', currency)
4551
}
4652

0 commit comments

Comments
 (0)