Netbasic Facts. |

Nov/08

25

Getting Rid of Decimal Places in PHP

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.

No tags

1 comment

  • Author comment by james · November 26, 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);

Leave a Reply

You must be logged in to post a comment.

<<

>>

Theme Design by devolux.nh2.me