Jump to content

Recommended Posts

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'?

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.

$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

 

 

 

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+

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.