mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-18 02:36:31 +00:00
[UI] Improve formatting functions (#10025)
* Improve formatting functions - Add try/catch around formatting functions - Better logic for default values * Change fallback logic
This commit is contained in:
@@ -22,6 +22,7 @@ export function formatDecimal(
|
||||
return value;
|
||||
}
|
||||
|
||||
try {
|
||||
const formatter = new Intl.NumberFormat(locale, {
|
||||
style: 'decimal',
|
||||
maximumFractionDigits: options.digits ?? 6,
|
||||
@@ -29,6 +30,11 @@ export function formatDecimal(
|
||||
});
|
||||
|
||||
return formatter.format(value);
|
||||
} catch (e) {
|
||||
console.error('Error formatting decimal:', e);
|
||||
// Return the unformatted value if formatting fails
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -61,14 +67,20 @@ export function formatCurrencyValue(
|
||||
const minDigits = options.minDigits ?? 0;
|
||||
const maxDigits = options.digits ?? 6;
|
||||
|
||||
try {
|
||||
const formatter = new Intl.NumberFormat(locale, {
|
||||
style: 'currency',
|
||||
currency: options.currency,
|
||||
currency: options.currency || 'USD',
|
||||
maximumFractionDigits: Math.max(minDigits, maxDigits),
|
||||
minimumFractionDigits: Math.min(minDigits, maxDigits)
|
||||
});
|
||||
|
||||
return formatter.format(value);
|
||||
} catch (e) {
|
||||
console.error('Error formatting currency:', e);
|
||||
// Return the unformatted value if formatting fails
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@inventreedb/ui",
|
||||
"description": "UI components for the InvenTree project",
|
||||
"version": "0.3.0",
|
||||
"version": "0.3.1",
|
||||
"private": false,
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
|
@@ -27,14 +27,13 @@ export function formatCurrency(
|
||||
|
||||
// Extract default digit formatting
|
||||
options.digits =
|
||||
options?.digits ?? Number(global_settings.PRICING_DECIMAL_PLACES) ?? 6;
|
||||
options?.digits || (Number(global_settings.PRICING_DECIMAL_PLACES) ?? 6);
|
||||
options.minDigits =
|
||||
options?.minDigits ??
|
||||
Number(global_settings.PRICING_DECIMAL_PLACES_MIN) ??
|
||||
0;
|
||||
options?.minDigits ||
|
||||
(Number(global_settings.PRICING_DECIMAL_PLACES_MIN) ?? 0);
|
||||
|
||||
options.currency =
|
||||
options?.currency ?? (global_settings.INVENTREE_DEFAULT_CURRENCY || 'USD');
|
||||
options?.currency || global_settings.INVENTREE_DEFAULT_CURRENCY || 'USD';
|
||||
|
||||
return formatCurrencyValue(value, options);
|
||||
}
|
||||
|
Reference in New Issue
Block a user