title: How can I locale-format a python Decimal and preserve its precision?
body: <p>How can I format a <code>Decimal</code> using a locale?</p>
  
  <p>To describe by examples, I'm trying to define a function <code>f</code> such that in the US English locale:</p>
  
  <ul>
  <li><p><code>f(Decimal('5000.00')) == '5,000.00'</code></p></li>
  <li><p><code>f(Decimal('1234567.000000')) == '1,234,567.000000'</code></p></li>
  </ul>
  
  <p>Some things that don't work:</p>
  
  <ul>
  <li><p><code>f = str</code> doesn't use locale; <code>f(Decimal('5000.00')) == '5000.00'</code></p></li>
  <li><p><code>f = lambda d: locale.format('%f', d)</code> doesn't preserve the decimal precision; <code>f(Decimal('5000.00')) == '5000.000000'</code></p></li>
  <li><p><code>f = lambda d: locale.format('%.2f', d)</code> uses a fixed precision, which isn't what I'm after; <code>f(Decimal('1234567.000000')) == '1234567.00'</code></p></li>
  </ul>