8

example: 1.123 =>1 1.999 => 1

thanks.

lovespring
  • 19,051
  • 42
  • 103
  • 153

6 Answers6

31
$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";
selfawaresoup
  • 15,473
  • 7
  • 36
  • 47
  • 2
    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 May 15 '10 at 18:06
  • 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. – selfawaresoup May 15 '10 at 18:19
  • Or just apply intval() – Armen Mar 28 '22 at 11:00
27
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
  • 3,014
  • 1
  • 23
  • 24
  • 2
    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 May 15 '10 at 18:52
3

You could use a bitwise operator.

Without:

echo 49 / 3;
>> 16.333333333333

With "| 0" bitwise:

echo 49 / 3 | 0;
>> 16
Rafalages
  • 1,376
  • 1
  • 9
  • 19
  • man your biopic is so similar to Basel, search for #freebasel, and you'll get me, BTW thanks for the great answer – Sarout Feb 01 '22 at 10:40
3
$y = 1.234;
list($y) = explode(".", "$y");
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
nothrow
  • 15,882
  • 9
  • 57
  • 104
  • 3
    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... – selfawaresoup May 15 '10 at 17:59
2

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
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 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. – selfawaresoup May 15 '10 at 18:05
  • @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 May 15 '10 at 18:07
  • @Mark: True. Didn't think of that. I posted (int) just for performance reasons :) – selfawaresoup May 15 '10 at 18:11
  • @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 May 15 '10 at 18:12
  • @Mark: Sure thing. Amazing how several developers can talk about such a trivial thing like float->int conversion for such a long time ;) – selfawaresoup May 15 '10 at 18:20
  • @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 May 15 '10 at 18:22
0

To remove all number after point use some php function

echo round(51.5); // Round the number, return 51.
echo floor(51.5); // Round down number, return 51.
echo ceil(51.3); // Round up number, return 52.
Shorabh
  • 190
  • 2
  • 9