tags:

views:

123

answers:

4

example: 1.123 =>1 1.999 => 1

thanks.

+10  A: 
floor() 

will round a number down to the nearest integer.

EDIT: As pointed out by Mark below, this will only work for positive values, which is an important assumption. For negative values, you'd want to use ceil() -- but checking the sign of the input value would be cumbersome and you'd probably want to employ Mark's or TechnoP's (int) cast idea instead. Hope that helps.

LesterDove
I'm not sure if this applies to PHP, but the (int)-cast could yield some overflow...Ah, http://stackoverflow.com/questions/300840/force-php-integer-overflow tells us: PHP max int is 2147483647.
osti
+7  A: 
$y = 1.235251;
$x = (int)$y;
echo $x; //will echo "1"

Edit: Using the explicit cast to (int) is the most efficient way to to this AFAIK. Also casting to (int) will cut off the digits after the "." if the number is negative instead of rounding to the next lower negative number:

echo (int)(-3.75); //echoes "-3";
echo floor(-3.75); //echoes "-4";
Techpriester
explicit cast is faster then floor() indeed, however, when such microoptimazations are of your concern, then you shouldn't be using PHP in the first place.
Pim Jager
I disagree, if I'm using PHP, I already have a big performance drop just by using an interpreted language. It's still in my responsibility as a developer, not to introduce more unnecessary lag. It's also an issue of memory since floor() is a function call and by that will require additional memory. Depending on the situation, the performance impact may even be quite significant.
Techpriester
+1  A: 
$y = 1.234;
list($y) = explode(".", "$y");

:-)

Yossarian
Well, if you want to win an obfuscation contest, this would be the way to go ... Funny, but better don't use this for real...
Techpriester
+2  A: 

If your input can only be positive floats then as already mentioned floor works.

floor(1.2)

However if your integer could also be negative then floor may not give you what you want: it always rounds down even for negative numbers. Instead you can cast to int as another post mentioned. This will give you the correct result for both negative and positive numbers.

(int)-1.2
Mark Byers
Even with a string, floor() will work because PHPs dynamic typing as long as the string contains no weird characters besides digits and a ".". Firing up preg for this will just lead to slow performance and a waste of memory.
Techpriester
@Techpriester: OK removed. But I still think you should use int, not floor if the input could be negative. No-one else has considered negative inputs yet, which I think is a shame because the poster did not say that the input must be positive.
Mark Byers
@Mark: True. Didn't think of that. I posted (int) just for performance reasons :)
Techpriester
@Techpriester: Perhaps you should edit your post to include this then I'll vote up yours. It looks very unlikely that my answer will be seen by anyone so it's unlikely to get any votes.
Mark Byers
@Mark: Sure thing. Amazing how several developers can talk about such a trivial thing like float->int conversion for such a long time ;)
Techpriester
@Mark, I agree that that's an important distinction; I've edited my answer to reflect all that. @Techno, thx for all the other good info. Nothing's simple as it seems...
LesterDove