[newbie] decimal precision

David Ascher da at ski.org
Mon Sep 6 18:50:01 EDT 1999


> Hello all.
> 
> I was wondering if anyone knows how to limit decimal precision in
> Python. Here is an example of where my problem occurs:
> 
> >>> 3.0 / 7
> 0.428571428571
> 
> Lets say I wanted to limit the value it returns to 3 decimal places, for
> example. How would that be done?

One of the most under-appreciated builtins does a good job at it:

>>> round(3.0/7, 3)
0.429
>>> round(3.0/7, 5)
0.42857
>>> round(3.0/7, 1)
0.4
>>> round(3.0/7, 2)
0.43

--david ascher





More information about the Python-list mailing list