Client: Improved form number parser

This commit is contained in:
Donkie
2023-08-12 20:19:18 +02:00
parent e81bf8b8e7
commit 16bed68b90

View File

@@ -6,5 +6,15 @@ export function numberFormatter(value: number | undefined) {
// Number parser that supports both comma and dot as decimal separator // Number parser that supports both comma and dot as decimal separator
export function numberParser(value: string | undefined) { export function numberParser(value: string | undefined) {
return Number(value?.replace(",", ".") ?? 0); // Convert comma to dot
const decimalSeparator = (1.1).toLocaleString().charAt(1);
if (decimalSeparator === ",") {
value = value?.replace(",", ".");
}
// Remove all non-digit characters
value = value?.replace(/[^\d.-]/g, "");
// Parse as float
return parseFloat(value || "0");
} }