[Tutor] floats

Michael bridges micha_el2003 at yahoo.com
Tue Jun 7 22:10:02 CEST 2011


ok, will attempt to clarify.
i want to out put of two numbers [int or float or anything] to be x.xx not x.x.
i want two numbers after the decimal not one.


--- On Tue, 6/7/11, Alan Gauld <alan.gauld at btinternet.com> wrote:

> From: Alan Gauld <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] floats
> To: tutor at python.org
> Date: Tuesday, June 7, 2011, 1:16 AM
> 
> "Michael bridges" <micha_el2003 at yahoo.com>
> wrote
> 
> > i want to 10 / 1000 and get 0.01 not 0
> > if 1000 is made 1000.00 then 0.01 is printed
> > but that gives 500 / 1000.00 is 0.5 not 0.50
> > 
> > can someone till me how to get a two decimal precision
> every time?
> 
> You are confusing two different things..
> The first case is that of integer versus float division.
> You solve that by either explicitly making one of the
> numbers a float or by converting to float using the
> foloat() operation.
> 
> The second issue is the *representation* of the result.
> The number of decimal places displayed is a matter
> of representation only, the actual value stored will not
> change. Thus 0.5 and 0.50 and 0.50000000 are all
> the same value in memory, it is only how they are
> printed that changes and that is controlled by how
> you choose to format the display.
> 
> Typically you use a format string:
> 
> res = 10/float(20)
> "%f" % res
> "%7.3f" % res
> "%5.1f" % res
> "%6e" % res
> "%6.4g" % res
> 
> 
> HTH,
> 
> -- Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list