set decimal place

Chris Barker chrishbarker at attbi.com
Thu Dec 27 14:40:58 EST 2001


Peter Hansen wrote:

> >>> 10.0/3
> 3.3333333333333335
> >>> round(10.0/3, 4)
> 3.3332999999999999
> ^^^^^^^
> This doesn't really solve the OP's problem.

> The concept of "having 4 decimal places" is meaningless with
> floating point representations until you actually display the
> value as a formatted string.

not completely. you might want to check if two values are the same to 4
decimal places, in which case round should work just fine:

>>> 10.0/3 == 10.00001/3
0
>>> round(10.0/3,4) == round(10.00001/3,4)
1

of course, you would more often want to compare given n significatn
figures instaead, in whioch case you can write a function with log, etc
that will do it for you. See the archives for a discussion of this, and
some posted code.

-Chris




-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at attbi.net                ---           ---           ---
                                     ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list