LukeKelvinSiiKeithJonChrisIanAnneColinJakeCarolineJamesHannahChristopher

Blog Words about the things we get up to

Read up on the latest news, gossip and goings on here at Netbasic!

Getting Rid of Decimal Places in PHP

posted by james on November 25th, 2008

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.

 
  1. November 25th, 2008 at 11:51 am

    Indeed a couple of people pointed out on my blog of the same post, it would be much quicker to do:

    $out = floor($val);