Truncate
Moderator: General Moderators
-
HiddenS3crets
- Forum Contributor
- Posts: 119
- Joined: Fri Apr 22, 2005 12:23 pm
- Location: USA
Truncate
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
-
RobertPaul
- Forum Contributor
- Posts: 122
- Joined: Sun Sep 18, 2005 8:54 pm
- Location: OCNY
Code: Select all
$truncatedNumber = (int)$nonTruncatedNumber;Don't use type-casting, it's very unelegant.
Try this instead:
Try this instead:
Code: Select all
$num = 105.643;
$truncated = intval($num);-
RobertPaul
- Forum Contributor
- Posts: 122
- Joined: Sun Sep 18, 2005 8:54 pm
- Location: OCNY
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
if you prefer to round, use round() even though it's not what you asked for, I thought it was worthy of a mention
Since there's so many suggestions, I'm feeling compelled to do a really obsure one.
...or an even more absurd one...
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;