Fix format for integer amounts (#231)

* support both fractions and integer values

* test fractions

* prettier
This commit is contained in:
Mert Demir
2024-09-29 22:24:10 +09:00
committed by GitHub
parent 8eea062218
commit 8742bd59da
3 changed files with 17 additions and 5 deletions

View File

@@ -28,10 +28,16 @@ export function formatCategoryForAIPrompt(category: Category) {
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(
currency: string,
amount: number,
locale: string,
fractions?: boolean,
) {
const format = new Intl.NumberFormat(locale, {
minimumFractionDigits: 2,
@@ -40,7 +46,7 @@ export function formatCurrency(
// '€' will be placed in correct position
currency: 'EUR',
})
const formattedAmount = format.format(amount)
const formattedAmount = format.format(fractions ? amount : amount / 100)
return formattedAmount.replace('€', currency)
}