title: Compare decimals in python
body: <p>I want to be able to compare Decimals in Python. For the sake of making calculations with money, clever people told me to use Decimals instead of floats, so I did. However, if I want to verify that a calculation produces the expected result, how would I go about it?</p>
  
  <pre><code>&gt;&gt;&gt; a = Decimal(1./3.)
  &gt;&gt;&gt; a
  Decimal('0.333333333333333314829616256247390992939472198486328125')
  &gt;&gt;&gt; b = Decimal(2./3.)
  &gt;&gt;&gt; b
  Decimal('0.66666666666666662965923251249478198587894439697265625')
  &gt;&gt;&gt; a == b
  False
  &gt;&gt;&gt; a == b - a
  False
  &gt;&gt;&gt; a == b - Decimal(1./3.)
  False
  </code></pre>
  
  <p>so in this example a = 1/3 and b = 2/3, so obviously b-a = 1/3 = a, however, that cannot be done with Decimals.</p>
  
  <p>I guess a way to do it is to say that I expect the result to be 1/3, and in python i write this as</p>
  
  <pre><code>Decimal(1./3.).quantize(...)
  </code></pre>
  
  <p>and then I can compare it like this:</p>
  
  <pre><code>(b-a).quantize(...) == Decimal(1./3.).quantize(...)
  </code></pre>
  
  <p>So, my question is: Is there a cleaner way of doing this? How would you write tests for Decimals?</p>