aebstract Posted September 18, 2008 Share Posted September 18, 2008 Okay this is a very beginner question but I just can't think of how to do this atm, I have a number: 1.52524233 but I want it to be 1.52, how do I just remove the remaining numbers from my value? Thanks Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 If you want to round it, just use round($number,2) for 2 decimal places. If you just want to truncate the rest of the decimal places, you'll have to create a function that does that. Quote Link to comment Share on other sites More sharing options...
aebstract Posted September 18, 2008 Author Share Posted September 18, 2008 I don't want to round, I need to keep my numbers pretty exact. I was using a rounding method to do this and while I was testing possibilities I ran in to a problem which is why I need to just remove the numbers now. edit: is there an easy way to remove all characters of a string except for the first two? Quote Link to comment Share on other sites More sharing options...
akitchin Posted September 18, 2008 Share Posted September 18, 2008 the manual will help you with the details, but you want one of round(), ceil(), float(), or number_format() depending on how you want the chopping to occur. http://www.php.net is your best friend. EDIT: beaten to the punch, but you don't need to create a function. one already exists for truncating decimals. Quote Link to comment Share on other sites More sharing options...
aebstract Posted September 18, 2008 Author Share Posted September 18, 2008 I don't want to round my number though, and number format seems to only place a comma in my number. If I go that way the only thing I can see doing is to put a comma every two numbers and explode and use the first value in my array. That'd be doing a little bit more than I wanted to but at least I can do that if I have to. Quote Link to comment Share on other sites More sharing options...
aebstract Posted September 18, 2008 Author Share Posted September 18, 2008 Well, after looking at number_format a little bit more, it just goes in thousands (3 places) which won't help me keep the first 2 separated from the rest of the pack. EDIT: knew this was something very easy, and I have found the solution:: substr('5054056412', 0, 2); cake Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 Maybe this: <?php $number = $number * 100; $number = floor($number); $number = $number / 100; ?> Or, simplified: <?php $number = (floor($number * 100))/100; ?> Quote Link to comment Share on other sites More sharing options...
aebstract Posted September 18, 2008 Author Share Posted September 18, 2008 substr(); is the correct solution Quote Link to comment Share on other sites More sharing options...
akitchin Posted September 18, 2008 Share Posted September 18, 2008 ... you probably didn't read the manual entry correctly for number_format(): echo number_format(1.52524233, 2); from the manual: decimals Sets the number of decimal points. no reason to use substr(). Quote Link to comment Share on other sites More sharing options...
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.