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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.