[PHP] Rounding down?
Is there a way to round down to the nearest 50? Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 would be 450; 500 to 549 would be 500, etc? The original number of subscribers is from a mySQL query and changes each day. I am trying to present a factual statement: There have been over ### subscribers in 2009 type of scenereo. Ron
Re: [PHP] Rounding down?
Hi, Is there a way to round down to the nearest 50? Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 would be 450; 500 to 549 would be 500, etc? Off the top of my head: divide the number by 50, run floor() on the result, then times it by 50. 1. 449 / 50 = 9.whatever 2. floor(9.whatever) = 9 3. 9 * 50 = 450 -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 8th August) Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding down?
On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote: Hi, Is there a way to round down to the nearest 50? Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 would be 450; 500 to 549 would be 500, etc? Off the top of my head: divide the number by 50, run floor() on the result, then times it by 50. 1. 449 / 50 = 9.whatever 2. floor(9.whatever) = 9 3. 9 * 50 = 450 -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 8th August) Lots of PHP and Javascript code - http://www.phpguru.org It should be round() and not floor(). 449 / 50 = 8.98 floor(8.98) = 8 8 * 50 = 400 round(8.98) = 9 9 * 50 = 450 Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding down?
Thanks; Amazing. Ron - Original Message - From: Ashley Sheridan a...@ashleysheridan.co.uk To: Richard Heyes rich...@php.net Cc: Ron Piggott ron@actsministries.org; php-general@lists.php.net Sent: Saturday, August 22, 2009 9:02 AM Subject: Re: [PHP] Rounding down? On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote: Hi, Is there a way to round down to the nearest 50? Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 would be 450; 500 to 549 would be 500, etc? Off the top of my head: divide the number by 50, run floor() on the result, then times it by 50. 1. 449 / 50 = 9.whatever 2. floor(9.whatever) = 9 3. 9 * 50 = 450 -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 8th August) Lots of PHP and Javascript code - http://www.phpguru.org It should be round() and not floor(). 449 / 50 = 8.98 floor(8.98) = 8 8 * 50 = 400 round(8.98) = 9 9 * 50 = 450 Thanks, Ash http://www.ashleysheridan.co.uk No virus found in this incoming message. Checked by AVG - www.avg.com Version: 8.5.409 / Virus Database: 270.13.64/2319 - Release Date: 08/22/09 06:06:00 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding down?
I also wrote a function for that, function round_to($number, $increments) { $increments = 1 / $increments; return (round($number * $increments) / $increments); } (Also published on php manual - round() ) On Sat, Aug 22, 2009 at 4:11 PM, Ron Piggott ron@actsministries.orgwrote: Thanks; Amazing. Ron - Original Message - From: Ashley Sheridan a...@ashleysheridan.co.uk To: Richard Heyes rich...@php.net Cc: Ron Piggott ron@actsministries.org; php-general@lists.php.net Sent: Saturday, August 22, 2009 9:02 AM Subject: Re: [PHP] Rounding down? On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote: Hi, Is there a way to round down to the nearest 50? Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 would be 450; 500 to 549 would be 500, etc? Off the top of my head: divide the number by 50, run floor() on the result, then times it by 50. 1. 449 / 50 = 9.whatever 2. floor(9.whatever) = 9 3. 9 * 50 = 450 -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 8th August) Lots of PHP and Javascript code - http://www.phpguru.org It should be round() and not floor(). 449 / 50 = 8.98 floor(8.98) = 8 8 * 50 = 400 round(8.98) = 9 9 * 50 = 450 Thanks, Ash http://www.ashleysheridan.co.uk No virus found in this incoming message. Checked by AVG - www.avg.com Version: 8.5.409 / Virus Database: 270.13.64/2319 - Release Date: 08/22/09 06:06:00 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Use ROT26 for best security
Re: [PHP] Rounding down?
Hi, It should be round() and not floor(). 449 / 50 = 8.98 floor(8.98) = 8 8 * 50 = 400 round(8.98) = 9 9 * 50 = 450 Not based on the examples given: Example: Any number between 400 and 449 I would 400 to be displayed -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 8th August) Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding down?
Hi, ... A little modification: ?php /** * Rounds down to the nearest 50 */ function myRound($val) { $units = intval(substr($val, -2)); return intval(substr($val, 0, -2) . ($units = 50 ? '50' : '00')); } echo myRound(449) . 'br /'; // 400 echo myRound(450) . 'br /'; // 450 echo myRound(356) . 'br /'; // 350 echo myRound(79) . 'br /'; // 50 ? PS I haven't checked if there's a PHP function for this. -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 8th August) Lots of PHP and Javascript code - http://www.phpguru.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding down?
On Sat, 22 Aug 2009 14:02:58 +0100, a...@ashleysheridan.co.uk (Ashley Sheridan) wrote: On Sat, 2009-08-22 at 13:00 +0100, Richard Heyes wrote: Hi, Is there a way to round down to the nearest 50? Example: Any number between 400 and 449 I would 400 to be displayed; 450 to 499 would be 450; 500 to 549 would be 500, etc? Off the top of my head: divide the number by 50, run floor() on the result, then times it by 50. 1. 449 / 50 = 9.whatever 2. floor(9.whatever) = 9 3. 9 * 50 = 450 -- Richard Heyes HTML5 graphing: RGraph - www.rgraph.net (updated 8th August) Lots of PHP and Javascript code - http://www.phpguru.org It should be round() and not floor(). 449 / 50 = 8.98 floor(8.98) = 8 8 * 50 = 400 round(8.98) = 9 9 * 50 = 450 Definitely floor or int, not round. Round 0.5-1.499 - 1 Floor 1.0-1.999 - 1 And if you really want to be accurate you should allow for decimal conversion errors in any operation involving floating point numbers. Otherwise you cannot rely on (say) 50 not being represented as 49.99, so that int or floor will give 49. In something like this I usually add half of the precision I want to work to: eg: $answer = 50* (int) (($x +0.5)/50); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] rounding down
hi everyone, i am looking for a snipplet to round down a number. i was wondering if you could put a negative number in the round() statement to do this, i want it to round down even if its at something.9, as long as its not a whole number, if needs to be rounded down. can anyone help me?
Re: [PHP] rounding down
Brad Ciszewski wrote: hi everyone, i am looking for a snipplet to round down a number. i was wondering if you could put a negative number in the round() statement to do this, i want it to round down even if its at something.9, as long as its not a whole number, if needs to be rounded down. can anyone help me? http://us4.php.net/floor -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding down
On November 26, 2004 08:58, Brad Ciszewski wrote: hi everyone, i am looking for a snipplet to round down a number. i was wondering if you could put a negative number in the round() statement to do this, i want it to round down even if its at something.9, as long as its not a whole number, if needs to be rounded down. can anyone help me? Searching on the php.net site in the manual is/can be your friend. You want the floor() function. Take a look at: http://ca3.php.net/manual/en/function.floor.php -- Regards, David Bevan We could learn a lot from crayons: some are sharp, some are pretty, some are dull, some have weird names, and all are different colorsbut they all exist very nicely in the same box. http://www.getanyideas.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding down
Brad Ciszewski wrote: hi everyone, i am looking for a snipplet to round down a number. i was wondering if you could put a negative number in the round() statement to do this, i want it to round down even if its at something.9, as long as its not a whole number, if needs to be rounded down. can anyone help me? floor() -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php