2
0
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:
Oliver
2025-07-15 14:33:08 +10:00
committed by GitHub
parent 471b8c239f
commit f12479dd37
3 changed files with 30 additions and 19 deletions

View File

@@ -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;
}
}
/*

View File

@@ -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",

View File

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