Rounding Decimals PHP

0

Greetings to the entire stack overflow community, could you help me with decimals rounding in PHP?

<?php

$bs = 10215251;

$formatbs = number_format($bs, 2, ',', '.');

$reconver = $bs / 1000;

$redondeo = round($reconver, 3, PHP_ROUND_HALF_UP);

$formatbss = number_format($redondeo, 3, ',', '.');

echo $formatbs." Bs<br>";

echo $formatbss." BsS<br>";
?>

When the third number (thousandth) is different from zero, the second number (hundredth) must be increased to the highest cent, but I can not do it: S ...

    
asked by michael Ramirez 21.05.2018 в 21:49
source

2 answers

0

Well the third place is hundredth, but as understood I share this solution.

    <?php

    $bs = 10215551;
    function redondear($bs){
        $numeroConDecimales = $bs/pow(10,3);
        $numeroSinDecimales = floor($numeroConDecimales);
        $soloDecimales = round($numeroConDecimales-$numeroSinDecimales,3);
        $redondeo = ceil($soloDecimales*pow(10,2));
        $number = $numeroSinDecimales+$redondeo/pow(10,2);
        return $number*pow(10,2);
    }
    $redondeadoCentena= redondear($bs);
    echo $redondeadoCentena;echo "\n";
    echo number_format($redondeadoCentena/1000, 3, ',', '.');

    ?>
    
answered by 21.05.2018 в 23:49
-1

As simple as this

$x=9.992929323;
echo round($x,0);
    
answered by 22.05.2018 в 19:26