On Sun, Jun 15, 2008 at 4:36 AM, Jarred Bishop <[EMAIL PROTECTED]> wrote:
> it returns '0'. which isnt much help. how do i get '0.5' or '.5' ?

This has a very long history, going all the way back to the C
programming language (which the Python interpreter is written in, and
which many currently-popular languages are, at least in part,
descended from). In C dividing an integer by an integer always yields
an integer, no matter what -- if you care about fractional/decimal
bits, you need to ensure that one or both of the numbers involved
supports having fractional/decimal bits.

Python has inherited this quirk, and so the following situation exists:

200 / 400 # yields 0
200.0 / 400 # yields 0.5
200 / 400.0 # yields 0.5
200.0 / 400.0 # yields 0.5

One possible solution, then, is to ensure that at least one of the
numbers involved is a floating-point number (Python type 'float')
instead of a whole integer (Python type 'int').

This behavior will be changing in Python 3.0, but will not be changed
in the Python 2.x line because it's backwards-incompatible. In Python
3.0:

200 / 400 # yields 0.5
200 // 400 # yields 0, because there are cases where "integer
division" is useful

Until then, you can selectively enable this by adding the following at
the top of any Python file where you're doing division:

from __future__ import division

This turns on the (backwards-incompatible with code which assumes
Python's standard integer division) behavior which will be standard in
Python 3.0.


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to