cliftonbazaar Posted April 18, 2009 Share Posted April 18, 2009 I tried to put this question in google but couldn't work out how to write it ??? How do I display a number without the fractions? for example I wish to display (111 divided by 7) and the answer is 15.857......, so how can I get it to just display '15'? Quote Link to comment https://forums.phpfreaks.com/topic/154587-solved-displaying-a-number-without-the-decimal/ Share on other sites More sharing options...
Philip Posted April 18, 2009 Share Posted April 18, 2009 You can with one of these functions: round, floor, ceiling Quote Link to comment https://forums.phpfreaks.com/topic/154587-solved-displaying-a-number-without-the-decimal/#findComment-812897 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 cast it as an int. it will chop it all off. $x = (int) (111/7); or you can use floor to always round down to whole number. or if you want to round up you can use ceil. or round will round up or down, to nearest whole number. Quote Link to comment https://forums.phpfreaks.com/topic/154587-solved-displaying-a-number-without-the-decimal/#findComment-812898 Share on other sites More sharing options...
seaweed Posted April 18, 2009 Share Posted April 18, 2009 $answer = round(111/7); echo '$answer'; // will display 16... it rounds up or down I believe... $answer = ceil(111/7); echo '$answer'; // will display 16 as it rounds up only. $answer = floor(111/7); echo '$answer'; // will display 15 as it rounds down only Quote Link to comment https://forums.phpfreaks.com/topic/154587-solved-displaying-a-number-without-the-decimal/#findComment-812902 Share on other sites More sharing options...
cliftonbazaar Posted April 18, 2009 Author Share Posted April 18, 2009 Thanks for all the replies, I'll bookmark this page in case I need the info again Quote Link to comment https://forums.phpfreaks.com/topic/154587-solved-displaying-a-number-without-the-decimal/#findComment-812918 Share on other sites More sharing options...
.josh Posted April 18, 2009 Share Posted April 18, 2009 or hey, how about this: $x = explode('.',$x); echo $x[0]; or preg_match('~^[^\.]*\.~',$x,$match); echo $match[0]; or $x = preg_replace('~\..*~','',$x); or this is always fun at parties: $x = strstr($x,'.',true); // php5.3+ Quote Link to comment https://forums.phpfreaks.com/topic/154587-solved-displaying-a-number-without-the-decimal/#findComment-812926 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.