|
|
 |
intval (PHP 3, PHP 4 , PHP 5) intval -- 获取变量的整数值
zak at php dot net
12-Aug-2000 04:38
intval converts doubles to integers by truncating the fractional component
of the number.
When dealing with some values, this can give odd
results. Consider the following:
print intval ((0.1 + 0.7) *
10);
This will most likely print out 7, instead of the expected
value of 8.
For more information, see the section on floating
point numbers in the PHP manual (http://www.php.net/manual/language.types.double.php)
Also
note that if you try to convert a string to an integer, the result is often
0.
However, if the leftmost character of a string looks like a
valid numeric value, then PHP will keep reading the string until a
character that is not valid in a number is encountered.
For
example:
"101 Dalmations" will convert to
101
"$1,000,000" will convert to 0 (the 1st character is
not a valid start for a number
"80,000 leagues ..." will
convert to 80
"1.4e98 microLenats were generated
when..." will convert to 1.4e98
Also note that only decimal
base numbers are recognized in strings.
"099" will
convert to 99, while "0x99" will convert to 0.
One
additional note on the behavior of intval. If you specify the base
argument, the var argument should be a string - otherwise the base will not
be applied.
For Example:
print intval (77, 8); //
Prints 77
print intval ('77', 8); // Prints 63
| |