"Michael bridges" <micha_el2...@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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to