Re: rounding problem
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 I came upon an alogrithm years ago somewhere that showed how to programmatically round numbers. Here it is: // num is the number we want to round. it may be int or fp. // what is what we want to round num to (ie nearest 10, 1/100, etc) // The rounding algorithm is: // 1) Divide num by what (the base we want to round to. ie .01 for hundredths, 10 for tens) // 2) Add .5 to num // 3) Truncate the result (keep the integer part) // 4) Multiply the result by num - -- /* All outgoing email scanned by AVG Antivirus */ Amer Neely, Softouch Information Services Home of Spam Catcher & North Bay Information Technology Networking Group W: www.softouch.on.ca E: [EMAIL PROTECTED] Perl | PHP | MySQL | CGI programming for all data entry forms. "We make web sites work!" -BEGIN PGP SIGNATURE- Version: GnuPG v1.0.6 (MingW32) Comment: For info see http://www.gnupg.org iEYEARECAAYFAkF+Aq4ACgkQ3RxspxLYVsXSMgCfRrMiiV6pmSxr3FqUPQcM3q+i ZqoAn3fgt75oz1C+vmL/G7kanC0ZNrfH =zQHq -END PGP SIGNATURE- -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: rounding problem
This must be a bug that was fixed in the 4.1 version. I see the same thing as Tom, using both 3.23.49 and 4.0.20... Oh, wait. The ever-helpful manual comes through again: From the manual documentation on ROUND(): "Note that the behavior of ROUND() when the argument is halfway between two integers depends on the C library implementation. Different implementations round to the nearest even number, always up, always down, or always toward zero. If you need one kind of rounding, you should use a well-defined function such as TRUNCATE() or FLOOR() instead." http://dev.mysql.com/doc/mysql/en/Mathematical_functions.html#IDX1363 There are many references to this "bug" in the MySQL Bug Tracker. All of them reinforce that this is not a bug but rather a variance in C library implementation. http://bugs.mysql.com/search.php?search_for=round&status=All&severity=all&limit=10&order_by=&cmd=display&direction=ASC&bug_type=Any&assign=&php_os=&phpver=&bug_age=0 Cheers, --V Edgar Meij wrote: Hmmm, peculiar... Tried it on 4.1.6-gamma-nt and works fine: SELECT ROUND(3.575, 2); = 3,57 SELECT ROUND(3.565, 2); = 3.56 The round() function probably cuts off the last bit... More info: http://lists.mysql.com/myodbc/8 Regards, Edgar -Oorspronkelijk bericht- Van: Tom Butterworth [mailto:[EMAIL PROTECTED] Verzonden: maandag 25 oktober 2004 19:35 Aan: [EMAIL PROTECTED] Onderwerp: rounding problem Hi I seem to be having problems returning the expected results when using the mysql ROUND() function. Rounding 3.565 to 2 decimal places i would expect to return 3.57 however using SELECT ROUND(3.565, 2); it returns 3.56. While using SELECT ROUND(3.575, 2); works as expected returning 3.58. I am using mysql version 3.23.54. Any help much appreciated. Cheers Buttie -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
RE: rounding problem
Hmmm, peculiar... Tried it on 4.1.6-gamma-nt and works fine: SELECT ROUND(3.575, 2); = 3,57 SELECT ROUND(3.565, 2); = 3.56 The round() function probably cuts off the last bit... More info: http://lists.mysql.com/myodbc/8 Regards, Edgar -Oorspronkelijk bericht- Van: Tom Butterworth [mailto:[EMAIL PROTECTED] Verzonden: maandag 25 oktober 2004 19:35 Aan: [EMAIL PROTECTED] Onderwerp: rounding problem Hi I seem to be having problems returning the expected results when using the mysql ROUND() function. Rounding 3.565 to 2 decimal places i would expect to return 3.57 however using SELECT ROUND(3.565, 2); it returns 3.56. While using SELECT ROUND(3.575, 2); works as expected returning 3.58. I am using mysql version 3.23.54. Any help much appreciated. Cheers Buttie -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED] -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
rounding problem
Hi I seem to be having problems returning the expected results when using the mysql ROUND() function. Rounding 3.565 to 2 decimal places i would expect to return 3.57 however using SELECT ROUND(3.565, 2); it returns 3.56. While using SELECT ROUND(3.575, 2); works as expected returning 3.58. I am using mysql version 3.23.54. Any help much appreciated. Cheers Buttie -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: mysql rounding problem
[ob. filter faker: mysql] For what it's worth, here's an algorithm I use to write my own rounding function. To round to the nearest N number of places. 1) divide the number by N 2) add .5 (for negative numbers subtract .5) 3) truncate the result (take integer part) 4) multiply result by N Example: To round 758.43 to nearest 10's 1) 758.43 / 10 = 75.843 2) 75.843 + .5 = 76.343 3) int(76.343) = 76 4) 76 * 10 = 760 Example: To round 758.437 to nearest 1/100th 1) 758.437 / 1/100 = 75843.7 2) 75843.7 + .5 = 75844.2 3) int(75844.2) = 75844 4) 75844 * 1/100 = 758.44 Example: To round -758.437 to nearest 1/100th 1) -758.437 / 1/100 = -75843.7 2) -75843.7 - .5 = -75844.2 3) int(-75844.2) = -75844 4) -75844 * 1/100 = -758.44 -- Amer Neely [EMAIL PROTECTED] Softouch Information Services: www.softouch.on.ca/ Perl / PHP / CGI programming for shopping carts, data entry forms. "We make web sites work!" - 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