title: Round off dict values to 2 decimals
body: <p>I'm having a hard time rounding off values in dicts. What I have is a list of dicts like this:</p>
  
  <pre><code>y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': 80.73246, 'b': 0.0, 'c':   
  10.780323, 'd': 10.0}, {'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a': 
  80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]
  </code></pre>
  
  <p>I need to round off the values to just 2 decimal places. </p>
  
  <p>When I try the following:</p>
  
  <pre><code>def roundingVals_toTwoDeci(y):
  
      for d in y:
          for k, v in d.items():
              v = ceil(v*100)/100.0
              print v
              d[k] = v
      return
  roundingVals_toTwoDeci(y)
  s = json.dumps(y)
  print s
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>0.0
  0.0
  18.2
  0.0
  27.3
  54.5
  0.0
  0.0
  0.0
  [{"a": 0.0, "b": 0.0, "c": 27.300000000000001, "d": 0.0, "e": 54.5, "f": 0.0, "g": 18.199999999999999, "h": 0.0, "i": 0.0}]
  </code></pre>
  
  <p>I need to make this work with versions 2.4+ and so am not using dict comprehensions. 
  First, I am having a hard time looping through all the key, values in all the dicts in the original. 
  Second, this result has just 1 decimal point instead of 2 when it prints inside the function? 
  Third, why is the 'json.dumps' and then 'print' not showing the values from inside the function?</p>
  
  <p>EDIT:</p>
  
  <p>Working with @Mark Ransom's answer below, I get the desired o/p. However, I have to urlencode the <code>json.dumps</code> value and send it to a URL. At the URL, it decodes the values into all the decimal places. So, for example, if, <code>josn.dumps</code> gives {"a": 9.1}, the URL shows it (after urlencode) as <code>9.10034254344365</code>. The modified code is as below:</p>
  
  <pre><code>class LessPrecise(float):
      def __repr__(self):
          return str(self)
  
  def roundingVals_toTwoDeci(y):
      for d in y:
          for k, v in d.items():
              v = LessPrecise(round(v, 2))
              print v
              d[k] = v
  
  
  
  
  roundingVals_toTwoDeci(y)
  j = json.dumps(y)
  print j
  
  params = urllib.urlencode({'thekey': j}) 
  </code></pre>
  
  <p><code>print json.dumps</code> gives <code>{"a": 9.1}</code>
  At the URL after <code>urlencode</code>, it gives <code>9.1078667322034</code> instead of <code>9.1</code>as in the following:</p>
  
  <p>Output:::</p>
  
  <pre><code>100.0
  0.0
  0.0
  0.0
  100.0
  0.0
  0.0
  0.0
  81.8
  0.0
  18.2
  0.0
  90.0
  0.0
  0.0
  10.0
  [{"a": 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a":
  81.8,  "b": 0.0, "c": 18.2, "d": 0.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]
  </code></pre>
  
  <p>At the URL:</p>
  
  <pre><code>9.100000381469727
  </code></pre>
  
  <p>The JSON string after json.dumps()</p>
  
  <pre><code>[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a":  
  80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]
  </code></pre>
  
  <p>The urlencode string - after decoding at <a href="http://meyerweb.com/eric/tools/dencoder/" rel="nofollow">http://meyerweb.com/eric/tools/dencoder/</a></p>
  
  <pre><code>thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d": 
  0.0}, {"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]
  </code></pre>
  
  <p>At the URL, I get values like <code>18.200000762939453</code>(this value is from a later script run)</p>