TUM-Dev/eat-api

Calculate prices in cent integers

lukasdenk opened this issue · 5 comments

Since floats are mathmatically uncountable, Python does not always calculate them correctly. This problem occurs, e.g. with some prices in the FMIParser.
E.g.:

Python Console
4.6+0.8
5.3999999999999995

Solution:

Calculate prices in cents. Then format them to a string containing the euro-format.

COM8 commented

This is a breaking change. Although I definitely see the need for this "fix" I would like to postpone it for an eventual V3 of the API.

How would you generate a string prince in python, since you probably will have the same problem, like you have shown here.

@COM8: I would calculate the prices in cent in a variable like priceInCents and make the price available as a @property. The property then always calculates the price from priceInCents.
Until v3, we could also make this property return a float.

COM8 commented

Yes, this would be an option.

Calculating the prices in integers of cents are a valid option.
However, i think we still should return numbers for the prices instead of already formatted strings.
As we always have at most two decimals for prices, we could use the round function from Python, to eliminate the rounding errors.

Even though, strings or numbers for prices probably wouldn't make a big difference at the moment, I still think for a good API, we shouldn't provide formatted values.

Price formatting should be done in frontend by a library specifically made for this purpose so changing this to cents in v3 is probably the best option