tags:

views:

88

answers:

3

I'm converting a .net app to php. What would be the php equivalent of int(6.22555445656) in php?

+1  A: 

This should clear it up:

<?php
header("Content-Type: text/plain");
$arr = array(-6, -6.2, -6.7, 5, 5.3, 5.8);
foreach ($arr as $v) {
  printf("%f: (int) = %d, floor = %d, ceil = %d\n", $v, (int)$v, floor($v), ceil($v));
}
?>

outputs:

-6.000000: (int) = -6, floor = -6, ceil = -6
-6.200000: (int) = -6, floor = -7, ceil = -6
-6.700000: (int) = -6, floor = -7, ceil = -6
5.000000: (int) = 5, floor = 5, ceil = 5
5.300000: (int) = 5, floor = 5, ceil = 6
5.800000: (int) = 5, floor = 5, ceil = 6

Note: Casting to int and floor() do different things.

Edit: Oh, you want to convert from a string to an int. One piece of advice: when you ask questions like these, describe what the original function does. You'll get a quicker and clearer answer.

Also, bear in mind that types in PHP and .Net work different. For example:

$foo = '-234'; // string
$bar = $foo + 5;
print $bar; // -229

PHP has a complex type juggling system to automatically convert the type of variables as required. This includes going to and from booleans, integers, floats and strings as required.

cletus
+1  A: 

I'm not certain what your goal is. But you can try

$var = (int) 6.22555445656;
print $var; // 6

Also see intval()

Mike B
+4  A: 

http://us3.php.net/manual/en/function.intval.php ??

jmucchiello
the interval() function was what I was looking for. Thank you!
shaiss
I'm confused--do you mean intval() is what you were looking for?
mgroves