Skip to content

Rounding issues? #490

@snyderjamie

Description

@snyderjamie

I have been trying to debug two parts of my code, with no success. I am not passing the Test Convert Target To Target with an AssertionError: AssertionError: assert 14.6615 == 14.6614 E + where 14.6615 = Currency(15.0, 'MXN').value. I think this is just a rounding error, but wanted to check.

Then secondly, I am not passing the test Test Add Methods with an AssertionError: assert 3.1496 == 4. This is more then a rounding error, but I am not seeing what is going wrong it my code.

Assignment: http://learn.rmotr.com/python/base-python-track/advanced-oop/currency-my-way-out

User code:

class Currency(object):
    USD_CONVERSIONS = {
        'CHF': 0.95,
        'CAD': 1.28,
        'GBP': 0.72,
        'JPY': 106.80,
        'EUR': 0.81,
        'USD': 1.0,
        'MXN': 18.62,
        'ARG': 20.24,
        'AUD': 1.27
    }

    def __init__(self, value, unit="USD"):
        if unit not in self.USD_CONVERSIONS:
            raise ValueError()
        self.value = value
        self.unit = unit

    def convert(self, target_unit):
        if target_unit not in self.USD_CONVERSIONS:
            raise ValueError()
        if self.unit == target_unit:
            return Currency(self.value, self.unit)
        result = (self.value / self.USD_CONVERSIONS[self.unit]
                * self.USD_CONVERSIONS[target_unit])
        self.value = round(result, 4)
        self.unit = target_unit
        return Currency(self.value, self.unit)
        

    def __str__(self):
        if self.value >= 10:
            return "{}${}".format(self.unit, round(self.value, 0))
        return "{}${}".format(self.unit, round(self.value, 1))

    def __repr__(self):
        if self.value >= 10:
            return "Currency({}, '{}')".format(round(self.value, 0), self.unit)
        return "Currency({}, '{}')".format(round(self.value, 1), self.unit)

    def __eq__(self, other):
        return self.convert(other.unit).value == other.value

    def __ne__(self, other):
        return self.convert(other.unit).value != other.value

    def __lt__(self, other):
        return self.convert(other.unit).value < other.value

    def __le__(self, other):
        return self.convert(other.unit).value <= other.value

    def __gt__(self, other):
        return self.convert(other.unit).value > other.value

    def __ge__(self, other):
        return self.convert(other.unit).value >= other.value

    def __add__(self, other):
        total = self.convert('USD').value + other.convert('USD').value
        return Currency(total, 'USD')

    def __iadd__(self, other):
        total = self.value + other.convert(self.unit).value
        self.value = total
        return Currency(self.value, self.unit)

    def __radd__(self, other):
        total = self.convert('USD').value + Currency(other).convert('USD').value
        return Currency(total, 'USD')

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions