Imagine a number in the European number format: 1.200,64 In US format, this would be 1,200.64
How can I detect the number format and if it's European format, convert it to US format.
A solution in either JavaScript or PHP would be needed.
You could match on the following regex pattern:
^\d{1,3}(?:\.\d{3})*,\d{2}$
and then do a replacement on such matches:
var input = "1.200.600";
var output = input.replace(/^(\d{1,3}(?:\.\d{3})*),?(\d*)$/, (x, y, z) => /,\d*$/.test(x) ? (y.replace(/\./g, ",") + "." + z) : y.replace(/\./g, ","));
console.log(input + " => " + output);
The regex pattern used here says to match:
^
from the start of the input
(
capture in $1
(which is y
in the above lambda)
\d{1,3}(?:\.\d{3})*
match a number with dot thousands separator)
,
match comma as decimal separator(
capture in $2
(which is z
in the above lambda
\d{2}
two digit decimal)
$
end of input