set decimal place
Peter Hansen
peter at engcorp.com
Wed Dec 26 14:48:53 EST 2001
Goh S H wrote:
> David Lees <debl2nononospammywhammy at bellatlantic.net> wrote in
> message news:<3C292938.ED322C60 at bellatlantic.net>...
> > kwsiew at tm.net.my wrote:
> > >
> > > is there a way(or any function) to set decimal place in python(to any
> > > decimal place I want)
> > > example :
> > > 2)10.0/3 = 3.3333333333333335 (I want 3.3333 - 4 decimal place)
>
> > >>> print '%8.1f' % (17.0/4)
> > 4.3
> > >>> print '%8.4f' % (10.0/4)
> > 2.5000
>
> OR use built-in function round if you're expecting float instead of string
>>> 10.0/3
3.3333333333333335
>>> round(10.0/3, 4)
3.3332999999999999
^^^^^^^
This doesn't really solve the OP's problem.
>>> print round(10.0/3, 4)
3.3333
>>> print "%.4f" % (10.0/3)
3.3333
The concept of "having 4 decimal places" is meaningless with
floating point representations until you actually display the
value as a formatted string.
--
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com
More information about the Python-list
mailing list