REPOST: Re: 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

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool1-nwblwi.newsops.execpc.com!newsfeeds.sol.net!newspump.sol.net!newsfeed.direct.ca!look.ca!nntp.kreonet.re.kr!feeder.kornet.net!news1.kornet.net!ua4canc3ll3r
From: Peter Hansen <peter at engcorp.com>
Newsgroups: comp.lang.python
Subject: cmsg cancel <3C2A29A5.8E9FB5BE at engcorp.com>
Control: cancel <3C2A29A5.8E9FB5BE at engcorp.com>
Date: Mon, 31 Dec 2001 02:55:50 GMT
Organization: A poorly-installed InterNetNews site
Lines: 2
Message-ID: <cancel.3C2A29A5.8E9FB5BE at engcorp.com>
NNTP-Posting-Host: 211.57.49.2
X-Trace: news2.kornet.net 1009776115 27193 211.57.49.2 (31 Dec 2001 05:21:55 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 05:21:55 +0000 (UTC)
X-No-Archive: yes
X-Unac4ncel: yes
X-Commentary: I love NewsAgent 1.10 and the Sandblaster Cancel Engine Build 74 (19 March 1999)

This message was cancelled from within Mozilla.



More information about the Python-list mailing list