Use the ROUND statement

ROUND(sum(CLICKS),sum(IMPS)/sum(CLICKS)*100.0,0),2);

-----Original Message-----
From: David Garamond [mailto:davegaramond@;icqmail.com]
Sent: Sunday, November 03, 2002 2:31 AM
To: [EMAIL PROTECTED]
Subject: rounding behaviour


hi,

mysql> select 1/29;
+------+
| 1/29 |
+------+
| 0.03 |
+------+
1 row in set (0.00 sec)

mysql> select 100.0*1/29;
+------------+
| 100.0*1/29 |
+------------+
|      3.448 |
+------------+
1 row in set (0.00 sec)

mysql> select 1/29*100.0;
+------------+
| 1/29*100.0 |
+------------+
|       3.45 |
+------------+
1 row in set (0.00 sec)

i am slightly puzzled by mysql's behaviour in the first case. this seems 
to be a float division, but why does mysql rounds it to two digits after 
decimal? if this is supposed to be an integer division, wouldn't it be 
better/more predictable for mysql to return 0?

this behaviour could cause subtle problem/errors because people really 
didn't expect this kind of behaviour. for example, i just found out 
today, after weeks of operation, that my sql expression:

  SELECT ...,if(sum(CLICKS),sum(IMPS)/sum(CLICKS)*100.0,0) as CTR FROM T

generates CTR that are rounded to two digits after decimal (yes, IMPS 
and CLICKS are integer fields). however, after i change the expression 
to this:

  SELECT ...,if(sum(CLICKS),100.0*sum(IMPS)/sum(CLICKS),0) as CTR FROM T

all is well.

--
dave


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail
<[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to