Jump to content

number format


kingb00zer

Recommended Posts

Hey I cant seem to think how to cut off most of the numbers after a decimal point. I want to make 0.0270721925134 look like 0.02 im sure its something simple too. I use the number format funtion sometimes but Im not sure how to get it to do that.

Link to comment
Share on other sites

That function returns a string, though. PHP has a proper number function that rounds off numbers:http://uk3.php.net/round
echo round(0.0270721925134, 2); // Returns 0.02

That will actually print 0.03I don't think PHP has a function that will truncate decimal points. I use this function to round to multiples. You can use it to truncate decimal points too.
function mround($number, $multiple, $round=0) {	//This function rounds a number to the nearest multiple of $multiple	//The $round argument specifies whether to round up, down, or whichever is closer	$value = 'NaN';	if (is_numeric($number) && is_numeric($multiple)) {		switch ($round) {			case -1: //Round down				$value = floor($number/$multiple)*$multiple;				break;			case 1: //Round up				$value = ceil($number/$multiple)*$multiple;				break;			default: //Round either up or down, whichever is closer				$value = round($number/$multiple)*$multiple;				break;		}	}	return $value;}

You'd use it like this:echo mround(0.0270721925134, .01, -1);

Link to comment
Share on other sites

Thanks guys, I had a feeling that I had done this once before (at school last year,) a simple round($variable, 2) did the trick. It was the ' , 2 ' that I forgot how to do.

Link to comment
Share on other sites

Thank you, these pages conatin some very interesting information, some things I could see myself using in the future.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...