I like to convert string with a price to a float value. The price comes from different languages and countries and can look like this:
1,00 €
€ 1.00
1'000,00 EUR
1 000.00$
1,000.00$
1.000,00 EURO
or whatever you can think of...
Not sure I got the full range of possibilities with my examples. I am also not sure if it is possible to make in international convert blindly, maybe I have to use a language code? So for the start Euro and Dollar would be enough.
floatval()
is kind of stupid so I need something more here. I think I should first remove all chars beside numbers, ,
and .
. Then fix the ,
/ .
and use floatval finally.
Has someone done this before and can help me a little?
I would prefer a solution without regexp ;)
1,000.00
which represents a thousand. You an also have1000.00
which is again 1 thousand. You can also have100,00
which is a hundred (in my country we don't use the dot for decimal separator). So the question is how to interpret prices you encounter, it can vary. You need to set some ground rules, such as what's decimal separator and what's thousands separator. After that it's trivial to determine the price - skip everything that's not.
or,
or numeric character.