galvin Posted June 15, 2010 Share Posted June 15, 2010 This is probably super easy, but if I have floats like "5.125", "9.0625", "12.3125", etc, etc, how can I get just the number to the left of the decimal point? If it was alway just one digit to the left, i could handle that (believe it or not ). But sometimes it's 1 digit, sometimes 2 digits, possibly even more than 2. So with those three examples, it would return... 5 9 12 And while I'm at it, I also need a way to get all the digits to the right of the decimal point. So also need a way to get... 125 0625 3125 I imagine the answer to one of these is also the answer to the other one, but I don't know either answer Can anyone help? Thanks, Greg Quote Link to comment https://forums.phpfreaks.com/topic/204795-getting-numbers-to-the-left-or-right-of-a-decimal/ Share on other sites More sharing options...
Cagecrawler Posted June 15, 2010 Share Posted June 15, 2010 $num = 3.141; $positionOfDecimalPoint = strpos($num, '.'); $numbersToLeft = substr($num,0,$positionOfDecimalPoint); $numbersToRight = substr($num,$positionOfDecimalPoint + 1);//Add one so decimal point isn't included in output. Quote Link to comment https://forums.phpfreaks.com/topic/204795-getting-numbers-to-the-left-or-right-of-a-decimal/#findComment-1072159 Share on other sites More sharing options...
galvin Posted June 15, 2010 Author Share Posted June 15, 2010 It's so logical once you see it!!! Thank you so much Cagecrawler, exactly what I needed! Quote Link to comment https://forums.phpfreaks.com/topic/204795-getting-numbers-to-the-left-or-right-of-a-decimal/#findComment-1072162 Share on other sites More sharing options...
mrMarcus Posted June 15, 2010 Share Posted June 15, 2010 Easiest way to get the number "on the left" would be to cast the float to an integer: <?php $num = 3.141; echo (int)$num; //outputs 3 An alternative to getting the digits to the right of the decimal: <?php $num = 3.141; $num_to_right = ((strpos($num, '.') !== false) ? end(explode('.', $num)) : $num); echo $num_to_right; //outputs 141 Quote Link to comment https://forums.phpfreaks.com/topic/204795-getting-numbers-to-the-left-or-right-of-a-decimal/#findComment-1072166 Share on other sites More sharing options...
galvin Posted June 15, 2010 Author Share Posted June 15, 2010 Thank you also, MrMarcus. By the way, your signature cracked me up (proclaiming next year's leafs as Stanley Cup champs). I'm a Flyers fan :'( Quote Link to comment https://forums.phpfreaks.com/topic/204795-getting-numbers-to-the-left-or-right-of-a-decimal/#findComment-1072168 Share on other sites More sharing options...
mrMarcus Posted June 15, 2010 Share Posted June 15, 2010 I'm a Flyers fan :'( Ouch. Still a little tender I'm sure. Quote Link to comment https://forums.phpfreaks.com/topic/204795-getting-numbers-to-the-left-or-right-of-a-decimal/#findComment-1072169 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.