[thelist] php - rounding decimals help

Andy Warwick mailing.lists at creed.co.uk
Thu Oct 28 09:58:42 CDT 2004


On 28 Oct 2004, at 15:06, Phil Cooper wrote:

>
> I'm fairly new to php and am trying to figure out how to round 
> numbers.  What I want to do is the equivalent of the excel roundup 
> function ie round up to one decimal place eg 3.34 is rounded to 3.4.
>
> I've been through the php manual and done a bit of googling and have 
> found the round() and ceil() functions but these don't do what I want.

?

Round *does* do what you want (kinda).

	float round ( float val [, int precision])

	echo round( 3.34, 1  ) ;			// 3.3

The key is the optional 2 parameter, introduced in PHP 4.

I guess the issue is the fact that you are wanting to round up, rather 
the 'correct' way which is up for .5 and above, down for less than .5.

In that case, I'd do something like:

	echo round( 3.34 + 0.05, 1  ) ;			// 3.4
	
And always add 0.05 to your number, to ensure you are always in the 
'upper' range.

HTH

-- 
Andy Warwick
Creed New Media. <http://www.creed.co.uk>



More information about the thelist mailing list