Formatting Note for 2 decimal places

0

Receiving four values of inputs type number and doing some operations I'm requiring that the returns be formatted to 2 decimal places forever. Anyone has any tips on how to do it, because I could not and with format_number the formatting got currency notation.

Follow the code below:

<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>";
                            }
                        }

                    ?>

Follow 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);
    }
}

?>
    
asked by anonymous 15.06.2017 / 06:18

1 answer

1

Since you are multiplying you can solve two problems at once, using 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));

Try this here.

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

If you want to use 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 . to , change to number_format($total, 2, ',', ''); .

    
15.06.2017 / 06:35