[PHP] PHP Rounding Question
I have a division formula that will return an value from 0.00 to 5... so I can get values like 2.38 or 4.79. However, never lower than 0 or higher than 5. How do I coerce the result to always round up to the nearest increment 1/4? So for example: 2.06 gets rounded to 2.25 0.01 gets rounded to 0.25 4.28 gets rounded to 4.50 3.71 gets rounded to 3.75 Thanks, --Rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP Rounding Question
On Fri, May 20, 2011 at 10:40 AM, Rick Dwyer rpdw...@earthlink.net wrote: How do I coerce the result to always round up to the nearest increment 1/4? $x = ceil($x * 4) / 4; David
[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 -- was [PHP] round to nearest 500?
At 8:40 PM +0100 2/13/07, Satyam wrote: - Original Message - From: Jon Anderson [EMAIL PROTECTED] The reason is simple: 0: No rounding. It's already there. (8.0 doesn't need to be rounded to 8 - it already *is* 8.) 1-4: You round down - 4 of 9 times you round down. 5-9: You round up - 5 of 9 times you round up. That is not quite correct, there is no such 4 ninths agains 5 ninths. You round down in the interval from 0 to0.4 and you round up from 0.5 to 0.999. If you substract the .5 from 0.9, you get 0.499 so it is about the same interval for both. If there is any difference, and actually there is, is because any number is, in reality, truncated, and not rounded, at some point. Depending on the number of bits of the mantissa, it might be a long way off, but eventually, it will happen, and that one is truncation, nor rounding, and if you repeat any calculation involving fractional numbers, it will eventually add up to something noticeable. Actually, there is the further problem that computers actually use binary, not decimal, so they cannot represent decimal numbers exactly. To offer an example of what I'm talking about, 1/3, which results in a never ending 0.33 is exactly 0.1 in base 3 arithmetic! Conversely, many 'round' numbers in our decimal notation are not 'round' in binary and viceversa. So it is all the piling of lots of rounding errors in real-life number representations that produce the error, not the mathematics of rounding, which actually work in an abstract world of infinitely precise number representations (in other words, infinite bits to represent any numbers). Satyam Satyam and Jon: Actually, both of you are correct. The bias in rounding comes from the concept of rounding down for numbers 0-4 and rounding up for number 5-9. Please, let's take a critical look at that premise. For zero, as Jon claims, there is no rounding at all -- you don't do anything. You can't include zero as one of the conditions that claims to do something when it doesn't do anything. It doesn't do anything to the data at all! So, to include zero in a rounding scheme is fundamentally flawed. Satyam, put your objections on hold for a moment and consider. For values one to four, you round down. So, you have four conditions where you actually do something to the data, you round down. For values five to nine, you round up. So, you have five conditions where you actually do something to the data, you round up. If you do anything four times one way and five times the other, you're going to introduce a bias. So, how do you get around this? One way is to use Statistical (Stochastic) rounding. It singles out five as being the culprit for this bias. It uses the even/odd rule of the preceding digit to determine which direction to round 5. If the value is even, then it rounds up -- whereas if the value is odd, then it rounds down. (or reverse the rounding, it doesn't matter as long as you are consistent). For all other values (1-4 and 6-9, note four each direction) the typical rounding is observed. Examples of it at work: 0.5 - 1 (zero is treated as even -- see below *) 1.5 - 1 2.5 - 3 3.5 - 3 4.5 - 5 5.5 -5 6.5 - 7 7.5 - 7 8.5 - 9 9.5 - 9 From this distribution (actually all you'll ever need to prove this), you can see we have just as many cases that round up as we do that round down -- thus, the aforementioned rounding bias has been reduced. The negative range is just a mirror image. Now, one can try to pump this algorithm through a PHP loop to see the difference (as I've tried) but you'll find that there are rounding errors (due to what Satyam said about computers representing decimal numbers exactly) that will prohibit making the algorithm work as described. For example, the built-in functions, such as intval(), do not work the way you might expect them to work. Try using this statement: $t =intval(($number - intval($number)) * 10); It works fine if the number is 89441560.5 -- it returns 5. However, if the number is 139690837.6 -- returns 5 instead of 6. And the list goes on of examples where the result isn't quite what's expected. If you look deeper into this, you'll find the rounding problem doesn't really have a solution, but rather it's a compromise. I'm sure that Rasmus Lendorf could shed some light on this -- remember the When is z != z? argument I started many moons ago? I think this is along the same lines. Perhaps the round() function takes all this into account, but I don't know and therein lies my suspicion. Cheers, tedd *By definition, any number that can be divided by two and leaves no remainder is even. Some say that the value is undefined, but for sake of this argument, it's considered even. Besides, symmetry in nature is more desirable. -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To
[PHP] rounding test fails when I try to make test
Hi, I am trying to build the latest stable release of php (4.3.10) I've successfully compiled but when I run make test the following are the two bugs that fail. FAILED TEST SUMMARY - Bug #24142 (round() problems) [ext/standard/tests/math/bug24142.phpt] Bug #25694 (round() and number_format() inconsistency) [ext/standard/tests/math/bug25694.phpt] I've been searching for the past few days and haven't been able find any resolution. From looking at the Changelog for 4.3.10 http://www.php.net/ChangeLog-4.php it shows that both of these bugs were fixed in the 4.3.10 release. But still the tests fail? Are the bugs fixed and the test case hasn't been updated? My PHP app relies heavily on round(). Any insight into this issue is much appreciated. Thanks, Larry
[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
[PHP] rounding average to one decimal point
Hi, I have a randon group of numbers I need the average of. When I add them up and divide by how many there are and print the result, I get a lot of decimal places. The number comes out to look like 29.3529411765, but I don't need that many decimal places. rounding to one decimal place will be fine. anyway to trim off the excess decimal values? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding average to one decimal point
Hello Adam, Monday, May 10, 2004, 7:03:36 PM, you wrote: AW Hi, I have a randon group of numbers I need the average of. When I add AW them up and divide by how many there are and print the result, I get a AW lot of decimal places. The number comes out to look like 29.3529411765, AW but I don't need that many decimal places. rounding to one decimal place AW will be fine. anyway to trim off the excess decimal values? Try the round() function? :) ceil() and floor() might help if you decide you don't want the extra decimal places too. -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding average to one decimal point
How about round() echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 http://www.phpbuilder.com/manual/function.round.php Hi, I have a randon group of numbers I need the average of. When I add them up and divide by how many there are and print the result, I get a lot of decimal places. The number comes out to look like 29.3529411765, but I don't need that many decimal places. rounding to one decimal place will be fine. anyway to trim off the excess decimal values? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding average to one decimal point
On Mon, 10 May 2004, Richard Davey wrote: Hello Adam, Monday, May 10, 2004, 7:03:36 PM, you wrote: AW Hi, I have a randon group of numbers I need the average of. When I add AW them up and divide by how many there are and print the result, I get a AW lot of decimal places. The number comes out to look like 29.3529411765, AW but I don't need that many decimal places. rounding to one decimal place AW will be fine. anyway to trim off the excess decimal values? Try the round() function? :) ceil() and floor() might help if you decide you don't want the extra decimal places too. i didn't know about round(), guess i should check php.net/round next time ;) thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Rounding issue
I need to know if there is a work around for the following scenerio... $x = 4.5012412; echo round($x, 2); // results in 4.5 --- however I want 4.50! I want 2 decimal places! Is there a way to keep the zero from being dropped? I have not been able to get the zero to stay there ... I realize this is the interpreters problem but I need the zero. thanks in advance -- Kevin Ison Charlotte, NC -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding issue
$x = 4.5012412; echo round($x, 2); // results in 4.5 --- however I want 4.50! I want 2 decimal places! echo number_format ( round ($x, 2), 2); Larry -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding issue
* Thus wrote Kevin Ison ([EMAIL PROTECTED]): I need to know if there is a work around for the following scenerio... $x = 4.5012412; echo round($x, 2); // results in 4.5 --- however I want 4.50! I want 2 decimal places! Is there a way to keep the zero from being dropped? I have not been able to get the zero to stay there ... I realize this is the interpreters problem but I need the zero. number_format(round($x, 2),2); you can always control with printf functions, but number format is designed specifically for that. Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding issue
ahhh ok thanks guys!! That worked ... I knew it was something but I could not remember which function... thanks again! Larry E . Ullman [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] $x = 4.5012412; echo round($x, 2); // results in 4.5 --- however I want 4.50! I want 2 decimal places! echo number_format ( round ($x, 2), 2); Larry -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding issue
ahhh ok thanks guys!! That worked ... I knew it was something but I could not remember which function... thanks again! Curt Zirzow [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] * Thus wrote Kevin Ison ([EMAIL PROTECTED]): I need to know if there is a work around for the following scenerio... $x = 4.5012412; echo round($x, 2); // results in 4.5 --- however I want 4.50! I want 2 decimal places! Is there a way to keep the zero from being dropped? I have not been able to get the zero to stay there ... I realize this is the interpreters problem but I need the zero. number_format(round($x, 2),2); you can always control with printf functions, but number format is designed specifically for that. Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] PHP Rounding Time
I apologize in advance for my constant nub questions.. How can I round the current time up or down to the nearest 1/2 hour? eg: timeranges: 0900 through 0915 == 0900 0916 through 0930 == 0930 0931 through 0945 == 0930 0946 through 1000 == 1000 Jay Fitzgerald, Design Director Bayou Internet - http://www.bayou.com Toll Free: 888.30.BAYOU (22968) Vox: 318.338.2034 / Fax: 318.338.2506 E-Mail: [EMAIL PROTECTED] ICQ: 38823829 / AIM: bayoujf / MSN: bayoujf / Yahoo: bayoujf -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: PHP Rounding Time
I haven't quite thought this through, but I think something like this: $remainder = $ts % 60; if( $remainder 15 ) { $ts = $ts - $remainder; }else if( $remainder 15 $remainder 30 ) { $ts = $ts + (30 - $remainder); }else if( $remainder 30 $remainder 45 ) { $ts = $ts - ($remainder - 30); }else if( $remainder = 45 ) { $ts = $ts + (60 - $remainder); } something close to that I think... On Fri, 4 Apr 2003, Jay Fitzgerald wrote: I apologize in advance for my constant nub questions.. How can I round the current time up or down to the nearest 1/2 hour? eg: timeranges: 0900 through 0915 == 0900 0916 through 0930 == 0930 0931 through 0945 == 0930 0946 through 1000 == 1000 Jay Fitzgerald, Design Director Bayou Internet - http://www.bayou.com Toll Free: 888.30.BAYOU (22968) Vox: 318.338.2034 / Fax: 318.338.2506 E-Mail: [EMAIL PROTECTED] ICQ: 38823829 / AIM: bayoujf / MSN: bayoujf / Yahoo: bayoujf -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP Rounding Time
Jay Fitzgerald wrote: I apologize in advance for my constant nub questions.. How can I round the current time up or down to the nearest 1/2 hour? eg: timeranges: 0900 through 0915 == 0900 0916 through 0930 == 0930 0931 through 0945 == 0930 0946 through 1000 == 1000 I'm sure there's a better way to do this, but : $time = {any range} if (substr($time,2,1) 3) { $time = 0.substr($time,0,1)+1.00; } else { $time = 0.substr($time,0,1).00; } Untested -- Burhan Khalid phplist[at]meidomus[dot]com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Rounding
How do I round a number to the nearest 10 or even 5. Say I have a number like 12. Is there an easy way to round that up to 15 or 20? Robbert van Andel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding
At 22:17 20.02.2003, Van Andel, Robbert spoke out and said: [snip] How do I round a number to the nearest 10 or even 5. Say I have a number like 12. Is there an easy way to round that up to 15 or 20? [snip] function round_to($number, $full, $isup=true) { $factor = $number % $full; if ($isup) { if ($factor) $factor = $full - $factor; $result = $number + ($factor ? $factor : 0); } else $result = $number - $factor; return $result; } $x = 12; $to = 5; echo $x = , round_to($x, $to), \n, $x = , round_to($x, $to, false); -- O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding
On Friday 21 February 2003 05:17, Van Andel, Robbert wrote: How do I round a number to the nearest 10 or even 5. Say I have a number like 12. Is there an easy way to round that up to 15 or 20? To round to the nearest 5: Divide by 5 Round to the nearest integer Multiply by 5 I'll leave you to work out how to round to nearest 10. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* Man belongs wherever he wants to go. -- Wernher von Braun */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding
At 2/21/2003 05:29 AM, Jason Wong wrote: To round to the nearest 5: Divide by 5 Round to the nearest integer Multiply by 5 Modulus by 5 Subtract the result from the original number. Of course, my way depends on whether you're rounding up or down. -- S. Keller UI Engineer The Health TV Channel, Inc. (a non - profit organization) 3820 Lake Otis Pkwy. Anchorage, AK 99508 907.770.6200 ext.220 907.336.6205 (fax) Email: [EMAIL PROTECTED] Web: www.healthtvchannel.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] rounding...sort of
Hi everyone, I have a nubmer: 4.1 but I only want the whole number 4, even if it's 4.9, so this rules out using round (Unless I missed a parameter). How could I do this.. I'm drawing a blank... Pete -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding...sort of
On Saturday 28 December 2002 17:49, Peter Lavender wrote: Hi everyone, I have a nubmer: 4.1 but I only want the whole number 4, even if it's 4.9, so this rules out using round (Unless I missed a parameter). How could I do this.. I'm drawing a blank... floor() -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* Remark of Dr. Baldwin's concerning upstarts: We don't care to eat toadstools that think they are truffles. -- Mark Twain, Pudd'nhead Wilson's Calendar */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding...sort of
php.net/floor OR ? list($w,$d) = split('.','4.9'); echo $w; ? Justin French on 28/12/02 8:49 PM, Peter Lavender ([EMAIL PROTECTED]) wrote: Hi everyone, I have a nubmer: 4.1 but I only want the whole number 4, even if it's 4.9, so this rules out using round (Unless I missed a parameter). How could I do this.. I'm drawing a blank... Pete -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding...sort of
Use explode() please On Sun, 29 Dec 2002, Justin French wrote: php.net/floor OR ? list($w,$d) = split('.','4.9'); echo $w; ? Justin French on 28/12/02 8:49 PM, Peter Lavender ([EMAIL PROTECTED]) wrote: Hi everyone, I have a nubmer: 4.1 but I only want the whole number 4, even if it's 4.9, so this rules out using round (Unless I missed a parameter). How could I do this.. I'm drawing a blank... Pete -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding a number
Jason Wong wrote: On Monday 24 June 2002 11:34, Phil Schwarzmann wrote: I want to round a number to the nearest decimal place... if the number is 4.623, I want it to display 4.6 if the number is 2.36, I want it to display 2.7 You don't really mean 2.36 -- 2.7 ?? Is there a function that does this? round(), ceil(), floor() don't do this and I've checked through all the math functions in my handy-dandy PHP Functions reference book. round() seems to work. What's the problem you're having? It's worth noting that round doesn't always work e.g. try round(0.35,1) -- Jason Wong - Gremlins Associates - www.gremlins.com.hk Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* That's no moon... -- Obi-wan Kenobi */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding a number
seems to work fine for me. what are your results when you do this? mine are 0.4 and this is what is should be. if it were .349 it would round down. isn't this how it should work? What were your results? Jim Lucas - Original Message - From: George Whiffen [EMAIL PROTECTED] To: [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Monday, June 24, 2002 2:36 AM Subject: Re: [PHP] rounding a number Jason Wong wrote: On Monday 24 June 2002 11:34, Phil Schwarzmann wrote: I want to round a number to the nearest decimal place... if the number is 4.623, I want it to display 4.6 if the number is 2.36, I want it to display 2.7 You don't really mean 2.36 -- 2.7 ?? Is there a function that does this? round(), ceil(), floor() don't do this and I've checked through all the math functions in my handy-dandy PHP Functions reference book. round() seems to work. What's the problem you're having? It's worth noting that round doesn't always work e.g. try round(0.35,1) -- Jason Wong - Gremlins Associates - www.gremlins.com.hk Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* That's no moon... -- Obi-wan Kenobi */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding a number
On Tuesday 25 June 2002 02:16, Jim lucas wrote: seems to work fine for me. what are your results when you do this? mine are 0.4 and this is what is should be. if it were .349 it would round down. isn't this how it should work? What were your results? try round(0.35,1) I get 0.3 on php 4.0.6. -- Jason Wong - Gremlins Associates - www.gremlins.com.hk Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* Elegance and truth are inversely related. -- Becker's Razor */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] rounding a number
I want to round a number to the nearest decimal place... if the number is 4.623, I want it to display 4.6 if the number is 2.36, I want it to display 2.7 Is there a function that does this? round(), ceil(), floor() don't do this and I've checked through all the math functions in my handy-dandy PHP Functions reference book. Thanks for your help!!! Or..if it's too hard to do that, I could just use a function that chops off the end of some decimals, like... if the number is 2.343234, I want just 2.3 or if the number is 2.545434534534534534, I want just 2.5 Thanks!!
RE: [PHP] rounding a number
according to the pdf version of the manual I have, round() is what you need float round(float val[, int precision]) have another look that the man page -Original Message- From: Phil Schwarzmann [mailto:[EMAIL PROTECTED]] Sent: Monday, June 24, 2002 1:35 PM To: [EMAIL PROTECTED] Subject: [PHP] rounding a number I want to round a number to the nearest decimal place... if the number is 4.623, I want it to display 4.6 if the number is 2.36, I want it to display 2.7 Is there a function that does this? round(), ceil(), floor() don't do this and I've checked through all the math functions in my handy-dandy PHP Functions reference book. Thanks for your help!!! Or..if it's too hard to do that, I could just use a function that chops off the end of some decimals, like... if the number is 2.343234, I want just 2.3 or if the number is 2.545434534534534534, I want just 2.5 Thanks!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] rounding a number
I think what you're looking for is number_format() - you can set decimal places with it and used in combo with round(), ceil(), and/or floor(), you should be able to achieved the desired result. http://www.php.net/manual/en/function.number-format.php HTH, Jason Soza -Original Message- From: Phil Schwarzmann [mailto:[EMAIL PROTECTED]] Sent: Sunday, June 23, 2002 7:35 PM To: [EMAIL PROTECTED] Subject: [PHP] rounding a number I want to round a number to the nearest decimal place... if the number is 4.623, I want it to display 4.6 if the number is 2.36, I want it to display 2.7 Is there a function that does this? round(), ceil(), floor() don't do this and I've checked through all the math functions in my handy-dandy PHP Functions reference book. Thanks for your help!!! Or..if it's too hard to do that, I could just use a function that chops off the end of some decimals, like... if the number is 2.343234, I want just 2.3 or if the number is 2.545434534534534534, I want just 2.5 Thanks!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] rounding a number
On Monday 24 June 2002 11:34, Phil Schwarzmann wrote: I want to round a number to the nearest decimal place... if the number is 4.623, I want it to display 4.6 if the number is 2.36, I want it to display 2.7 You don't really mean 2.36 -- 2.7 ?? Is there a function that does this? round(), ceil(), floor() don't do this and I've checked through all the math functions in my handy-dandy PHP Functions reference book. round() seems to work. What's the problem you're having? -- Jason Wong - Gremlins Associates - www.gremlins.com.hk Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* That's no moon... -- Obi-wan Kenobi */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Rounding....
Seeing as the mathematically correct way to round numbers is to round down to n for n-1=m=n.5 and up to n+1 for n.5mn+1, I wonder why the PHP round() function couldn't include a little 'fuzz' to handle the rounding problems we encounter due to floating point representation in the hardware? It could even be a configurable option - but it would save writing a wrapper... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Rounding....
I guess that's because nobody knows beforehand which direction the fuzz should go - should it go a little upwards or a little downwards in order to match all systems? Just my two cents. Bogdan Matthew Clark wrote: Seeing as the mathematically correct way to round numbers is to round down to n for n-1=m=n.5 and up to n+1 for n.5mn+1, I wonder why the PHP round() function couldn't include a little 'fuzz' to handle the rounding problems we encounter due to floating point representation in the hardware? It could even be a configurable option - but it would save writing a wrapper... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Rounding a number up
Is there a way to round a number to the next whole number? Example: Before: 1.86758 After: 2 Thank you,
Re: [PHP] Rounding a number up
http://www.php.net/manual/en/function.ceil.php - Original Message - From: Brandon Orther [EMAIL PROTECTED] To: PHP User Group [EMAIL PROTECTED] Sent: Monday, September 10, 2001 2:19 PM Subject: [PHP] Rounding a number up Is there a way to round a number to the next whole number? Example: Before: 1.86758 After: 2 Thank you, -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] rounding up
What is the best seamless way to upgrade/update PHP? Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] rounding up
Is there a PHP command to round up to the nearest integer? J -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] rounding up
Hi Is there a PHP command to round up to the nearest integer? www.php.net/ceil www.php.net/round HTH M@ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] rounding up
Is there a PHP command to round up to the nearest integer? www.php.net/ceil www.php.net/round These are the correct functions, but take great care in reading the user contributed notes in the round function concerning x.5 rounding randomness. Sometimes this can get you. If you want to be sure 4.5 always rounds to 5, add a fudge factor smaller than your precision, but larger than the precision of the computer. $value = 4.5; $fudge = 0.001; round($value) sometimes returns 5 round($value) sometimes returns 4 $fudgedvalue = $value + $fudge; round($fudgedvalue) = 5 This comes into play often when you do things like split a long list of items into two columns on your web page and you use PHP (or any language) to figure out where to end the first column and start the second. It should be noted that this is not a PHP issue. It affects any language which relies on the hardware to compute. Fun stuff eh? John -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Rounding to strange results
Hi, I use a product database for 2 sites, one of which adds sales tax and rounds to the nearest .10 and on the other site there is no tax and also no rounding. I am unable to figure out why a price of 1000.00 is being displayed as 1.00 and any price over 1000 does the same thing. 100.00 works fine, as does all other prices, this is the same with and without rounding. Anything I should look for to solve this problem? Martin. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Rounding to strange results
Is there a comma in one thousand, e.g., 1,000.00? round() will truncate everything to the right of a comma. Kirk -Original Message- From: Martin E. Koss [mailto:[EMAIL PROTECTED]] Sent: Tuesday, March 06, 2001 7:45 AM To: [EMAIL PROTECTED] Subject: [PHP] Rounding to strange results Hi, I use a product database for 2 sites, one of which adds sales tax and rounds to the nearest .10 and on the other site there is no tax and also no rounding. I am unable to figure out why a price of 1000.00 is being displayed as 1.00 and any price over 1000 does the same thing. 100.00 works fine, as does all other prices, this is the same with and without rounding. Anything I should look for to solve this problem? Martin. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Rounding to strange results
You can write a function that get the %(mod) of 1000 then subtracts it from the number. Rick At 08:43 AM 3/6/01 -0700, Johnson, Kirk wrote: Is there a comma in one thousand, e.g., 1,000.00? round() will truncate everything to the right of a comma. Kirk -Original Message- From: Martin E. Koss [mailto:[EMAIL PROTECTED]] Sent: Tuesday, March 06, 2001 7:45 AM To: [EMAIL PROTECTED] Subject: [PHP] Rounding to strange results Hi, I use a product database for 2 sites, one of which adds sales tax and rounds to the nearest .10 and on the other site there is no tax and also no rounding. I am unable to figure out why a price of 1000.00 is being displayed as 1.00 and any price over 1000 does the same thing. 100.00 works fine, as does all other prices, this is the same with and without rounding. Anything I should look for to solve this problem? Martin. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] ## # Rick St Jean, # [EMAIL PROTECTED] # President of Design Shark, # http://www.designshark.com/ # Quick Contact: http://www.designshark.com/messaging.ihtml # Tel: 905-684-2952 ## -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Rounding a number up if the number is anything more than a whole number
Hello, I am doing a math function where I divide one number by another. I want it to give me a whole number though, and if it is anything above a number I want it to go to the next one up. Example: 4.0001 would equal 5 3.98 would equal 4 11.023 would equal 12 I hope you understand what I am trying to say. Thank you, Brandon Orther WebIntellects Design/Development Manager [EMAIL PROTECTED] 800-994-6364 www.webintellects.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Rounding a number up if the number is anything more thana whole number
On Mon, 17 Jan 2000, Brandon Orther wrote: Hello, I am doing a math function where I divide one number by another. I want it to give me a whole number though, and if it is anything above a number I want it to go to the next one up. Example: 4.0001 would equal 5 3.98 would equal 4 11.023 would equal 12 I hope you understand what I am trying to say. Thank you, Brandon Orther WebIntellects Design/Development Manager [EMAIL PROTECTED] 800-994-6364 www.webintellects.com ceil() -- Ignacio Vazquez-Abrams [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]