coddingtonbear / django-measurement

Easily store, retrieve, and convert measurements of weight, volume, distance, area and more.
MIT License
145 stars 33 forks source link

Rounding error #93

Closed gabn88 closed 4 years ago

gabn88 commented 4 years ago

Thanks for this fine package.

I'm having a small issue, however, with the Temperature. When I enter 38,4 degrees Celsius, it gets saved as 38,39999999999998 degrees Celsius, which doesn't look so nice.

Is there a way to solve this?

coddingtonbear commented 4 years ago

I'm afraid that's a limitation rooted in floating-point numbers themselves. At the moment, the library providing these conversions (python-measurement) has that limitation: https://python-measurement.readthedocs.io/en/latest/#python-measurement (see 'Warning').

There is, though, a PR open for allowing optional use of Decimal values: https://github.com/coddingtonbear/python-measurement/pull/32 . You might want to follow-along there to see how you might be able to help.

gabn88 commented 4 years ago

Thank you!

JackAtOmenApps commented 4 years ago

@gabn88

Here's the output of the code in the python-measurement pull request, based on your example. It hasn't been merged yet, but hopefully soon.

>>> from measurement.measures import Temperature
>>> t = Temperature(c=38.4)
>>> t
Temperature(c=38.39999999999997)
>>> td = Temperature(c="38.4", decimal=True)
>>> td
Temperature(c=38.4000000000000)

Once that's merged, there's also a PR in django-measurement using the new python-measurement code and a DecimalField number base for the MeasurementField.

gabn88 commented 4 years ago

That's great news! Hope it gets merged before the weekend, so I can include it in our update :)