Jump to content

[SOLVED] Displaying a number without the decimal


cliftonbazaar

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+

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.