kingb00zer 0 Posted June 21, 2011 Report 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. Quote Link to post Share on other sites
trevelluk 0 Posted June 21, 2011 Report 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. Quote Link to post Share on other sites
Ingolme 1,032 Posted June 21, 2011 Report 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 Quote Link to post Share on other sites
ShadowMage 94 Posted June 21, 2011 Report 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); Quote Link to post Share on other sites
Ingolme 1,032 Posted June 21, 2011 Report 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. Quote Link to post Share on other sites
ShadowMage 94 Posted June 21, 2011 Report 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. Quote Link to post Share on other sites
kingb00zer 0 Posted June 22, 2011 Author Report 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. Quote Link to post Share on other sites
justsomeguy 1,135 Posted June 22, 2011 Report 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 Quote Link to post Share on other sites
kingb00zer 0 Posted June 23, 2011 Author Report Share Posted June 23, 2011 Thank you, these pages conatin some very interesting information, some things I could see myself using in the future. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.