kingb00zer Posted June 21, 2011 Share Posted June 21, 2011 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 More sharing options...
trevelluk Posted June 21, 2011 Share Posted June 21, 2011 The second parameter to number_format controls the number of digits to display after the decimal point. The manual page at http://uk3.php.net/number_format provides more information. Link to comment Share on other sites More sharing options...
Ingolme Posted June 21, 2011 Share Posted June 21, 2011 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 Link to comment Share on other sites More sharing options...
ShadowMage Posted June 21, 2011 Share Posted June 21, 2011 That function returns a string, though. PHP has a proper number function that rounds off numbers:http://uk3.php.net/roundecho 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 More sharing options...
Ingolme Posted June 21, 2011 Share Posted June 21, 2011 Yeah, that's right. Though for precision it's usually better to round it than to truncate or round upwards. Link to comment Share on other sites More sharing options...
ShadowMage Posted June 21, 2011 Share Posted June 21, 2011 Yeah, that's right. Though for precision it's usually better to round it than to truncate or round upwards.True, but there are occasionally applications for rounding up or truncating. Link to comment Share on other sites More sharing options...
kingb00zer Posted June 22, 2011 Author Share Posted June 22, 2011 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 More sharing options...
justsomeguy Posted June 22, 2011 Share Posted June 22, 2011 You can also use these for general-purpose number formatting:http://www.php.net/manual/en/function.number-format.phphttp://www.php.net/manual/en/function.sprintf.php Link to comment Share on other sites More sharing options...
kingb00zer Posted June 23, 2011 Author Share Posted June 23, 2011 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.