fix: accept nullable marketplace prices in formatter

This commit is contained in:
2026-04-21 21:01:53 -04:00
parent 651d54b837
commit e144dcabeb
4 changed files with 18 additions and 7 deletions

View File

@@ -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}`;
}
}