Home » Php » PHP How do I round down to two decimal places?

PHP How do I round down to two decimal places?

Posted by: admin November 29, 2017 Leave a comment

Questions:

I need to round down a decimal in PHP to two decimal places so that:

49.955

becomes…

49.95

I have tried number_format, but this just rounds the value to 49.96. I cannot use substr because the number may be smaller (such as 7.950). I’ve been unable to find an answer to this so far.

Any help much appreciated.

Answers:

This can work: floor($number * 100) / 100

Questions:
Answers:

Here is a nice function that does the trick without using string functions:

<?php
function floorp($val, $precision)
{
    $mult = pow(10, $precision);
    return floor($val * $mult) / $mult;
}

print floorp(49.955, 2);
?>

An other option is to subtract a fraction before rounding:

function floorp($val, $precision)
{
    $half = 0.5 / pow(10, $precision);
    return round($val - $half, $precision);
}

Questions:
Answers:

Multiply your input by 100, floor() it, then divide the result by 100.

Questions:
Answers:

Unfortunately, none of the previous answers (including the accepted one) works for all possible inputs.

1) sprintf('%1.'.$precision.'f', $val)

Fails with a precision of 2 : 14.239 should return 14.23 (but in this case returns 14.24).

2) floatval(substr($val, 0, strpos($val, '.') + $precision + 1))

Fails with a precision of 0 : 14 should return 14 (but in this case returns 1)

3) substr($val, 0, strrpos($val, '.', 0) + (1 + $precision))

Fails with a precision of 0 : -1 should return -1 (but in this case returns ‘-‘)

4) floor($val * pow(10, $precision)) / pow(10, $precision)

Although I used this one extensively, I recently discovered a flaw in it ; it fails for some values too. With a precision of 2 : 2.05 should return 2.05 (but in this case returns 2.04 !!)

So far the only way to pass all my tests is unfortunately to use string manipulation. My solution based on rationalboss one, is :

function floorDec($val, $precision = 2) {
    if ($precision < 0) { $precision = 0; }
    $numPointPosition = intval(strpos($val, '.'));
    if ($numPointPosition === 0) { //$val is an integer
        return $val;
    }
    return floatval(substr($val, 0, $numPointPosition + $precision + 1));
}

This function works with positive and negative numbers, as well as any precision needed.

You can find a working example here : http://codepad.viper-7.com/ZGprQJ.

Questions:
Answers:

Try the round() function

Like this: round($num, 2, PHP_ROUND_HALF_DOWN);

Questions:
Answers:
    function roundDown($decimal, $precision)
    {
        $fraction = substr($decimal - floor($decimal), 2, $precision); //calculates the decimal places to $precision length
        $newDecimal = floor($decimal). '.' .$fraction; // reconstructs decimal with new # of decimal places

        return floatval($newDecimal);
    }

    echo roundDown(345.8768378, 5); // OUTPUT: 345.87683

This function works with positive and negative numbers.

Code example here: http://codepad.org/RAOyBCuY

Questions:
Answers:

Use formatted output

sprintf("%1.2f",49.955) //49.95

DEMO

Questions:
Answers:

You can use:

$num = 49.9555;
echo substr($num, 0, strpos($num, '.') + 3);

Questions:
Answers:
sprintf("%1.2f",49.955) //49.95

if you need to truncate decimals without rounding – this is not suitable, because it will work correctly until 49.955 at the end, if number is more eg 49.957 it will round to 49.96

It seems for me that Lght`s answer with floor is most universal.

Questions:
Answers:

Did you try round($val,2) ?

More information about the round() function