.Net int () is equivalent in PHP

I am converting .net application to php. What would be the php equivalent int(6.22555445656)

in php?

+2


source to share


3 answers


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



+4


source


This should clear it:

<?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 for int and floor()

doing different things.



Edit: Oh, you want to convert from string to int. One tip: when you ask questions like this, describe what the original function does. You will receive a faster and more understandable answer.

Also, keep in mind that PHP and .NET types work differently. For example:

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

      

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

+1


source


I'm not sure what your purpose is. But you can try

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

      

Also see intval()

+1


source







All Articles