class Currency: """ allows calculations with a currency where the main unit is divided into hundredths. """ symbol = 'US$' def __init__(self, dollars, cents): if dollars >= 0: self.value = dollars*100+cents else: self.value = dollars*100-cents def __str__(self): if self.value > 0: return "{:s}{:d}.{:02d}".format(Currency.symbol,self.value//100, self.value%100) else: val = -self.value return "-{:s}{:d}.{:02d}".format(Currency.symbol,val//100, val%100) def __eq__(self, other): return self.value == other.value def __lt__(self, other): return self.value < other.value def __rmul__(self, factor): """we assume that factor is a floating point number """ value = round(self.value*factor) return Currency(value//100, value%100) def change_symbol(new_symbol): Currency.symbol = new_symbol