price-parser: "." incorrectly interpreted as a thousands separator
from price_parser import Price
print(Price.fromstring('3.350').amount_float)
print(Price.fromstring('3.3500').amount_float)
print(Price.fromstring('3.35').amount_float)
print(Price.fromstring('3.355').amount_float)
Results:
3350.0
3.35
3.35
3355.0
- python 3.8.6
- price-parser 0.3.4
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (10 by maintainers)
We could change the default behavior based on a locale, but I think allowing the flexibility to override the behavior would be best.
I would first try to agree on an interface. I think this should be an argument to
fromstring, not toamount_float, because I think this makes sense as part of the input, and it could help improve the parsing in some scenarios, while it would not make sense I think for someone to useamount_floatwithFalseandTruefor the same price object.@PyExplorer doing it based on currency makes sense, if different currencies have different rules. In addition to that, it also could make sense to provide an explicit parameter, to override any heuristics.
Sorry for the unclear explanation, here is what I mean,
The current
amount_floatproperty changes the “.” like a thousand separators when there are 3 digits after it, but what I mean is adding an optional input from the user saythousand_with_dotwhich by default is true, but if a user turnsthousand_with_dot = falsethen “.” will not act as a thousand separator when there are 3 digits after it.Example:
It is true that this will add a little work for the user but guessing it from the string provided by the user raises the possibility of error. Let me know your thoughts.
sorry @Manish-210 I’m not sure I understand your proposal - currently
amount_floatis a property which would return a float amount. But the problem here is that we don’t exactly know how to interpret “22.900”, and ideally we’d like to be able to make this decision without requiring extra input from the user.There are web-sites which use “.” as thousands separator, so if the number of digits after “.” is 3, then it’s interpreted as thousands. If you know the decimal separator (e.g. from locale / language), you can pass it, see https://github.com/scrapinghub/price-parser#decimal-separator