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,13 +22,19 @@ export function formatDecimal(
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatter = new Intl.NumberFormat(locale, {
|
try {
|
||||||
style: 'decimal',
|
const formatter = new Intl.NumberFormat(locale, {
|
||||||
maximumFractionDigits: options.digits ?? 6,
|
style: 'decimal',
|
||||||
minimumFractionDigits: options.minDigits ?? 0
|
maximumFractionDigits: options.digits ?? 6,
|
||||||
});
|
minimumFractionDigits: options.minDigits ?? 0
|
||||||
|
});
|
||||||
|
|
||||||
return formatter.format(value);
|
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 minDigits = options.minDigits ?? 0;
|
||||||
const maxDigits = options.digits ?? 6;
|
const maxDigits = options.digits ?? 6;
|
||||||
|
|
||||||
const formatter = new Intl.NumberFormat(locale, {
|
try {
|
||||||
style: 'currency',
|
const formatter = new Intl.NumberFormat(locale, {
|
||||||
currency: options.currency,
|
style: 'currency',
|
||||||
maximumFractionDigits: Math.max(minDigits, maxDigits),
|
currency: options.currency || 'USD',
|
||||||
minimumFractionDigits: Math.min(minDigits, maxDigits)
|
maximumFractionDigits: Math.max(minDigits, maxDigits),
|
||||||
});
|
minimumFractionDigits: Math.min(minDigits, maxDigits)
|
||||||
|
});
|
||||||
|
|
||||||
return formatter.format(value);
|
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",
|
"name": "@inventreedb/ui",
|
||||||
"description": "UI components for the InvenTree project",
|
"description": "UI components for the InvenTree project",
|
||||||
"version": "0.3.0",
|
"version": "0.3.1",
|
||||||
"private": false,
|
"private": false,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@@ -27,14 +27,13 @@ export function formatCurrency(
|
|||||||
|
|
||||||
// Extract default digit formatting
|
// Extract default digit formatting
|
||||||
options.digits =
|
options.digits =
|
||||||
options?.digits ?? Number(global_settings.PRICING_DECIMAL_PLACES) ?? 6;
|
options?.digits || (Number(global_settings.PRICING_DECIMAL_PLACES) ?? 6);
|
||||||
options.minDigits =
|
options.minDigits =
|
||||||
options?.minDigits ??
|
options?.minDigits ||
|
||||||
Number(global_settings.PRICING_DECIMAL_PLACES_MIN) ??
|
(Number(global_settings.PRICING_DECIMAL_PLACES_MIN) ?? 0);
|
||||||
0;
|
|
||||||
|
|
||||||
options.currency =
|
options.currency =
|
||||||
options?.currency ?? (global_settings.INVENTREE_DEFAULT_CURRENCY || 'USD');
|
options?.currency || global_settings.INVENTREE_DEFAULT_CURRENCY || 'USD';
|
||||||
|
|
||||||
return formatCurrencyValue(value, options);
|
return formatCurrencyValue(value, options);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user