Re: rounding a number

From: Date: Mon, 24 Jun 2002 09:44:06 +0000
Subject: Re: rounding a number
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-103562@lists.php.net to get a copy of this message
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 > > 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!!!!!! As I understand it, you just want to truncate the number, without rounding. I think you are right, there isn't a function to do it for you but the following code should do it: intval(4.623 * 10)/10 or more generally: function truncate_number($mynumber,$places) { return intval($mynumber * pow(10,$places))/pow(10,$places); }

« previous php.general (#103562) next »