mirror of
https://github.com/spliit-app/spliit.git
synced 2026-02-16 12:36:13 +01:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8742bd59da |
@@ -206,6 +206,7 @@ export function CreateFromReceiptButton({
|
|||||||
groupCurrency,
|
groupCurrency,
|
||||||
receiptInfo.amount,
|
receiptInfo.amount,
|
||||||
locale,
|
locale,
|
||||||
|
true,
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
@@ -57,10 +57,15 @@ describe('formatCurrency', () => {
|
|||||||
]
|
]
|
||||||
|
|
||||||
for (const variation of variations) {
|
for (const variation of variations) {
|
||||||
it(`formats ${variation.amount} in ${variation.locale}`, () => {
|
it(`formats ${variation.amount} in ${variation.locale} without fractions`, () => {
|
||||||
expect(formatCurrency(currency, variation.amount, variation.locale)).toBe(
|
expect(
|
||||||
variation.result,
|
formatCurrency(currency, variation.amount * 100, variation.locale),
|
||||||
)
|
).toBe(variation.result)
|
||||||
|
})
|
||||||
|
it(`formats ${variation.amount} in ${variation.locale} with fractions`, () => {
|
||||||
|
expect(
|
||||||
|
formatCurrency(currency, variation.amount, variation.locale, true),
|
||||||
|
).toBe(variation.result)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -28,10 +28,16 @@ export function formatCategoryForAIPrompt(category: Category) {
|
|||||||
return `"${category.grouping}/${category.name}" (ID: ${category.id})`
|
return `"${category.grouping}/${category.name}" (ID: ${category.id})`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param fractions Financial values in this app are generally processed in cents (or equivalent).
|
||||||
|
* They are are therefore integer representations of the amount (e.g. 100 for USD 1.00).
|
||||||
|
* Set this to `true` if you need to pass a value with decimal fractions instead (e.g. 1.00 for USD 1.00).
|
||||||
|
*/
|
||||||
export function formatCurrency(
|
export function formatCurrency(
|
||||||
currency: string,
|
currency: string,
|
||||||
amount: number,
|
amount: number,
|
||||||
locale: string,
|
locale: string,
|
||||||
|
fractions?: boolean,
|
||||||
) {
|
) {
|
||||||
const format = new Intl.NumberFormat(locale, {
|
const format = new Intl.NumberFormat(locale, {
|
||||||
minimumFractionDigits: 2,
|
minimumFractionDigits: 2,
|
||||||
@@ -40,7 +46,7 @@ export function formatCurrency(
|
|||||||
// '€' will be placed in correct position
|
// '€' will be placed in correct position
|
||||||
currency: 'EUR',
|
currency: 'EUR',
|
||||||
})
|
})
|
||||||
const formattedAmount = format.format(amount)
|
const formattedAmount = format.format(fractions ? amount : amount / 100)
|
||||||
return formattedAmount.replace('€', currency)
|
return formattedAmount.replace('€', currency)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user