View Code Snippet [PHP]

Get Round Off Amount

                                function round_off($number, $roundoff = true) {

    if (is_int($number)) {
        return $roundoff ? 0 : $number;
    }

    // Extract the decimal part of the number
    $decimal_part = $number - floor($number);

    // Check if $decimal_part is less than or equal to 0.49
    if ($decimal_part <= 0.49) {
        return $roundoff ? -$decimal_part : $number-$decimal_part;
    }

    // If $decimal_part is greater than 0.49
    $posRoundOff = 1 - $decimal_part;
    return $roundoff ? $posRoundOff : $number + $posRoundOff;
}