There’s a couple of ways to remove decimal places from a number in PHP…
$out = number_format($val,0); $out = (int) @floor($val);
Interestingly, we ran a real-world test case of 10 million iterations of doing each of these, and it turns out that method #2 is approximately 0.5 seconds quicker. So there you have it, if you want to be 0.5 seconds quicker when removing decimal places from a number 10 million times.. then use the second method.
November 25th, 2008 at 5:00 pm
Why the call to floor()?
for( $i = 1; $i ” . (int) $i . “\n”;
}
November 26th, 2008 at 10:07 am
Damn you David! I was just going to say that – you then remove the need to call the error suppression operator, which might save you another 0.5 seconds.
November 26th, 2008 at 10:14 am
I didn’t write the second way heh, I agree – it was our Technical team leader who did that haha… interpret that as you will
November 26th, 2008 at 12:57 pm
Sounds like the words in his job title need to be reordered by relevance
November 26th, 2008 at 4:33 pm
It was a “quick fix” for a live script
November 27th, 2008 at 12:29 am
Hmm… my example rendered as symbol-salad. Weird. Here’s a neat one we picked up somewhere (if it renders):
( string )( ( int )$x ) == $x
Returns false if $x is not an integer.