Truncate
Posted: Fri Dec 02, 2005 3:45 pm
Is there a function to truncate all the numbers after the decimal point so you're just left with a whole number?
ex: 105.643; truncated: 105
ex: 105.643; truncated: 105
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$truncatedNumber = (int)$nonTruncatedNumber;Code: Select all
$num = 105.643;
$truncated = intval($num);Code: Select all
$num = 105.43;
$pieces = split('.', "$num");
$truncated = $pieces[1];
echo $truncated;Code: Select all
$num = 105.43;
for ($i = 0; $i < strlen("$num"); $i++) {
if ("$num"{i} == '.') break;
$truncated .= intval("$num"{$i});
}
echo $truncated;Do I get bonus points for erroneous code?Jenk wrote:Your second snippet is so absurd it has parse errors!
Hint: it's $var{1} not "$var"{1} and you have missed out the dollar on the i