[Zope] Going CRAZY from division

Martijn Pieters mj@zope.com
Mon, 18 Nov 2002 17:29:41 -0500


On Mon, Nov 18, 2002 at 05:17:18PM -0500, BZ wrote:
> I have two integers "clicks" and "views" and am trying to find what the
> ratio of clicks to views. This should be (and is on my calculator)
> 
> clicks / views * 100   (to get the percent)
> 3 / 356 * 100 = .842 %
> 
> <dtml-var expr="clicks/views">  Returns = 0
> 
> <dtml-var expr="clicks/views" fmt="%.6f"> Returns 0.00000
> 
> I don't know what is wrong. Anyone?

Welcome to Python (and generally, most programming languages) integer
calculations where 1 / 2 is 0. Integer division will yield integer results.
If one of the operands is float however, it'll give a float result. In other
words, 1 / 2.0 is 0.5

You'll have to convert at least one of the two operands to a float for the
above to yield a float:

  <dtml-var expr="clicks/float(views)">

-- 
Martijn Pieters
| Software Engineer  mailto:mj@zope.com
| Zope Corporation   http://www.zope.com/
| Creators of Zope   http://www.zope.org/
---------------------------------------------