oanda / v20-python

OANDA v20 bindings for Python
MIT License
225 stars 91 forks source link

Support Python `Decimal` #38

Open HarryR opened 4 years ago

HarryR commented 4 years ago

The v20.Context class accepts a constructor parameter decimal_number_as_float=True

This flag is used in the Context.convert_decimal_number method, used by everything else to parse rational numbers returned in the API responses. By default it converts these all from strings to the native float type allowing arithmetic etc..

However, I would like the Python Decimal class to be used as it can guarantee exact precision when performing arithmetic.

One way to get around this is to sub-class the Context, e.g.

import v20
from decimal import Decimal

class DecimalContext(v20.Context):
    def convert_decimal_number(self, value):
        return Decimal(value)

References: