Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use decimals instead of floats #59

Merged
merged 2 commits into from
Apr 29, 2024
Merged

Conversation

naved001
Copy link
Collaborator

The tests for the usage report by each pod was updated so it matches the precision.

The tests for the usage report by each pod was updated so it matches the
precision.
@naved001 naved001 requested review from knikolla and QuanMPhm April 26, 2024 19:10
@naved001
Copy link
Collaborator Author

naved001 commented Apr 26, 2024

Edit: I added this example as a test to the code.

I generated the report for March again to see how much difference this would make. I noticed that for a couple of projects the cost were now more by 1 cent which was due to a combination of floating point math being imprecise followed by python's rounding.

Here's that scenario

>>> rate = 0.013
>>> hours = 4075
>>> rate*hours
52.974999999999994 # result is less than the actual value of 52.975
>>> round(rate*hours,2)
52.97

Using decimals

>>> from decimal import Decimal, ROUND_HALF_UP
>>> rate = Decimal("0.013")
>>> hours = 4075
>>> rate*hours
Decimal('52.975')
>>> (rate*hours).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
Decimal('52.98')

@naved001 naved001 merged commit be997dc into CCI-MOC:main Apr 29, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants