Latest Comments
No website changes have been recorded.
Search
Stuff I like
  • The Essays of Warren Buffett: Lessons for Corporate America, Second Edition
    The Essays of Warren Buffett: Lessons for Corporate America, Second Edition

    Read this review post

  • The Five Rules for Successful Stock Investing: Morningstar's Guide to Building Wealth and Winning in the Market
    The Five Rules for Successful Stock Investing: Morningstar's Guide to Building Wealth and Winning in the Market

    Read this review post

  • Programming Pearls (2nd Edition)
    Programming Pearls (2nd Edition)
  • Pattern Recognition and Machine Learning (Information Science and Statistics)
    Pattern Recognition and Machine Learning (Information Science and Statistics)
  • Programming Collective Intelligence: Building Smart Web 2.0 Applications
    Programming Collective Intelligence: Building Smart Web 2.0 Applications
  • Dyson DC25 Ball All-Floors Upright Vacuum Cleaner
    Dyson DC25 Ball All-Floors Upright Vacuum Cleaner
  • Sigma 30mm f/1.4 EX DC HSM Lens for Canon Digital SLR Cameras
    Sigma 30mm f/1.4 EX DC HSM Lens for Canon Digital SLR Cameras

Powered by Squarespace
« Mini-me | Main | Computer Go Advances »
Monday
18Aug2008

Seriously brain-dead way to round off numbers in Python

import decimal
def Round(value, precision = 3):
return float(str(
decimal.Decimal(str(value)).quantize(decimal.Decimal("1")
/ (decimal.Decimal('1' + '0' * precision)), decimal.ROUND_HALF_UP)))

Reader Comments (1)

I guess I should mention that if you really wanted to round off to 3 decimal places, you could just do.

import math
def Round(value, precision = 3):
return int(value * math.pow(10, precision)) / math.pow(10, precision)

July 9, 2009 | Unregistered Commenteryj

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>