-
Notifications
You must be signed in to change notification settings - Fork 152
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
Convert precision to localcontext #45
Convert precision to localcontext #45
Conversation
eth_utils/currency.py
Outdated
@@ -46,7 +43,7 @@ | |||
MAX_WEI = 2 ** 256 - 1 | |||
|
|||
|
|||
def from_wei(number, unit): | |||
def from_wei(number, unit, precision=999): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be configurable. It should just be a constant, otherwise, bad user decisions lead to imprecise monetary calculations.
if d_number < 1 and '.' in s_number: | ||
with localcontext() as ctx: | ||
multiplier = len(s_number) - s_number.index('.') - 1 | ||
ctx.prec = multiplier | ||
d_number = decimal.Decimal(value=number, context=ctx) * 10**multiplier | ||
unit_value /= 10**multiplier | ||
|
||
result_value = d_number * unit_value | ||
with localcontext() as ctx: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious if this one is needed. Do tests pass if it's removed? The d_number
already has the appropriate precision from the context above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two conversion_round_trip_*
tests fail without this. Specifically when the number
is '10000000000.000000000000000001'
it drops the trailing 1.
fced9ca
to
923cdc2
Compare
Note: I'll need to make a new |
What was wrong?
DefaultContext was being used to set precision.
How was it fixed?
Added precision as optional kwarg to allow users to set their own precision (@pipermerriam was this the right move?) and set precision using localcontext.
Cute Animal Picture
closes #44