Jump to content

rounding an integer


MiCR0

Recommended Posts

You may want to come up with a more meaningful name for it, but try this function:

 

function removeDecimalAndToSixPlaces($x)
{
    list($number, $decimal) = explode('.', $x);

    $decimal = substr($decimal, 0, 6);

    if (empty($number))
    {
        return (int) $decimal;
    }

    return (int) $number . $decimal;
}

echo removeDecimalAndToSixPlaces(52.71666666); //52716666
echo removeDecimalAndToSixPlaces(0.926888888); //926888

 

There's nothing particular complex within this, so you shouldn't have any performance problems runnning it 100,000 times. Although if this isn't something you plan on re-using, you should take it out of the function wrapper. sbustr couldn't be avoided as numer_format will round up the removed decimal points.

Link to comment
https://forums.phpfreaks.com/topic/223671-rounding-an-integer/#findComment-1156220
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.