fix: accept nullable marketplace prices in formatter
This commit is contained in:
@@ -5,9 +5,19 @@
|
||||
* @returns Formatted currency string
|
||||
*/
|
||||
export function formatCentsToCurrency(
|
||||
cents: number,
|
||||
cents: number | string | null | undefined,
|
||||
locale: string = "en-CA",
|
||||
): string {
|
||||
if (cents == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const numericCents =
|
||||
typeof cents === "string" ? Number.parseFloat(cents) : cents;
|
||||
if (!Number.isFinite(numericCents)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
try {
|
||||
const formatter = new Intl.NumberFormat(locale, {
|
||||
style: "currency",
|
||||
@@ -15,10 +25,10 @@ export function formatCentsToCurrency(
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
return formatter.format(cents / 100);
|
||||
return formatter.format(numericCents / 100);
|
||||
} catch {
|
||||
// Fallback if locale is not supported
|
||||
const dollars = (cents / 100).toFixed(2);
|
||||
const dollars = (numericCents / 100).toFixed(2);
|
||||
return `$${dollars}`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user