Format Note to 2 Decimal Places

Asked

Viewed 948 times

0

Receiving four input type values number and doing some operations I’m needing the returns to be formatted forever 2 decimal places. Does anyone have any tips on how to do it, because I couldn’t and with the format_number the formatting was with currency notation.

Follows code:

<form action="" method="POST" class="form">
                <h1>Calculo de Notas - Pitágoras</h1>
                    <h2>Parcial 01</h2><br/>
                    <input type="number" name="input1" class="input" max="10" step=0.01 maxlength="5" onkeyup="somenteNumeros(this);"><br/><br/>
                    <h2>Oficial 01</h2><br/>
                    <input type="number" name="input2" class="input" max="10" step=0.01 maxlength="5" onkeyup="somenteNumeros(this);"><br/><br/>
                    <?php
                        if(isset($total) && !empty($total)){
                            if($total >= 7){
                                echo "<div class='resultadoPositivo resultado' >".$total."</div>";
                            } else{
                                echo "<div class='resultadoNegativo resultado'>".$total."</div>";
                            }
                        }

                    ?>
                    <h2>Parcial 02</h2><br/>
                    <input type="number" name="input3" class="input" max="10" step=0.01 maxlength="5" onkeyup="somenteNumeros(this);"><br/><br/>
                    <h2>Oficial 02</h2><br/>
                    <input type="number" name="input4" class="input" max="10" step=0.01 maxlength="5" onkeyup="somenteNumeros(this);"><br/><br/>
                    <?php
                        if(isset($total1) && !empty($total1)){
                            if($total1 >= 7){
                                echo "<div class='resultadoPositivo resultado'> ".$total1."</div>";
                            } else{
                                echo "<div class='resultadoNegativo resultado'>".$total1."</div>";
                            }
                        }

                    ?>
                    <input type="submit" value="enviar" id="enviar" class="botao">
                </form>
                    <div class="container grid-16 " id="resultado">
                    <?php
                        if(isset($final) && !empty($final)){
                            if($final >= 7){
                                echo "<div class='resultadoPositivo resultadoFinal'>O seu resultado final foi: ".$final."</div>";
                                echo "<div class='   resultadoFinal on'>Parabéns</div>";
                            }else{
                                echo "<div class='resultadoNegativo resultadoFinal'>O seu resultado final foi: ".$final."</div>";
                                echo "<div class=' resultadoFinal off'>Que Pena! </div>";
                            }
                        }

                    ?>

Follows PHP:

if(isset($_POST['input1']) && !empty($_POST['input1'])){
    if(isset($_POST['input2']) && !empty($_POST['input2']) ){
        $nota1 = $_POST['input1'];
        $nota2 = $_POST['input2'];
        $total = ($nota1*0.3)+($nota2*0.7);

    }
}
if(isset($_POST['input3']) && !empty($_POST['input4'])){
    if(isset($_POST['input3']) && !empty($_POST['input4']) ){
        $nota1 = $_POST['input3'];
        $nota2 = $_POST['input4'];
        $total1 = ($nota1*0.3)+($nota2*0.7);

    }
}

if(isset($total) && !empty($total)){
    if(isset($total1) && !empty($total1)){
        $final = ($total*0.4)+($total1*0.6);
    }
}

?>

1 answer

1

Once it is multiplying you can solve two problems at once by using the Bcmath, after all float count "wrong".

Then set the number of houses:

bcscale(2);

Then do:

$nota1 = $_POST['input1'];
$nota2 = $_POST['input2'];

$total = bcadd(bcmul($nota1, '0.3'), bcmul($nota2, 0.7));

Test it out here.

Apply the same for other cases. The bcadd() will add up the results of bcmul(), they will all have two houses defined by bcscale().


If you want to use the number_format:

$nota1 = $_POST['input1'];
$nota2 = $_POST['input2'];

$total = ($nota1*0.3)+($nota2*0.7);

$total = number_format($total, 2, '.', '');

If you want to change the . for , change to number_format($total, 2, ',', '');.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.