Wednesday 23 May 2012

Rounding. The Way of Python

I cried a dozen of bloody tears trying to understand why the heck guys had it done this particular way.
Well, no more comments here. Just a hint: this piece of code is taken from a very enterprise UI and is supposed to round val with precision rdig. I'm not really a Python evangelist or something but isn't there anything around that rounds numbers?
   fmt = "%%.0%df" % rdig

   # Do rounding
   m = 1.0
   while rdig > 0:
      m *= 10.0
      rdig -= 1

   val = int(val * m + 0.5) / m
   val = fmt % val

No comments:

Post a Comment