How to format value in php?

Asked

Viewed 886 times

-4

I have the following code

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

include_once("con.php");

$pdo = conectar();

$id_empresa = $_GET['id_empresa'];
$data = $_GET['data'];

$tipo = "ENT";
$valorTotalE = 0;

$valorTotalEntradas=$pdo->prepare('SELECT valor FROM importa
                                                        WHERE data=:data
                                                        AND id_empresa=:id_empresa
                                                        AND tipo=:tipo');

$valorTotalEntradas->bindValue('id_empresa', $id_empresa);
$valorTotalEntradas->bindValue('data', $data);
$valorTotalEntradas->bindValue('tipo', $tipo);
$valorTotalEntradas->execute();

while ($linha=$valorTotalEntradas->fetch(PDO::FETCH_ASSOC)) {

    $valor = $linha['valor'];
    $valorTotalE = $valorTotalE + $valor;

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

    $return = array(
        'valorTotalE' => $valorTotalE
    );

}

echo json_encode($return);
?>

This command line "$valueTotalE = number_format($valueTotalE,2,','.');" formats the value in 9,999.99, however, it does this that appears in the image below

inserir a descrição da imagem aqui

  • For me the image seems correct with the example 9.999,99, point separates mile and comma separates the pennies. What exactly you expected to occur?

  • Where do you ask your question? What’s wrong ? =(

  • @Guilhermenascimentop. note that the value of 16,269.70 is to be the sum of the 4 values above.

  • @Rafaelsalomão, same answer I gave to Guilherme.

  • 2

    You are trying to add strings with non-numeric format, money/currency format is not number.

  • I make my own the words of @Guilhermenascimentop., you need to convert the value to float

  • If I do not place the line $valueTotalE = number_format($valueTotalE,2,','.'); the value appears added, but without this formatting.

Show 2 more comments

1 answer

2


The problem is you’re trying to add up after using the number_format, this makes no sense, you can only add what is number as strings that can do the cast or types int, float and bool

See if doing this will get the bug:

<?php

$valor1 = 'foo';
$valor2 = '2';
$valor3 = '3';

$total = $valor1 * $valor2 * $valor3;

In other words, the message indicates that you are trying to do a mathematical operation without having a valid number:

Notice: A non well Formed Numeric value encountered

Just use the number_format afterward:

while ($linha=$valorTotalEntradas->fetch(PDO::FETCH_ASSOC)) {

    $valor = $linha['valor'];
    $valorTotalE = $valorTotalE + $valor;

}

//Aqui você usa o number_format
$valorTotalE = number_format($valorTotalE, 2, ',', '.');

echo json_encode(array(
    'valorTotalE' => $valorTotalE
));

Note that for this to work in the database you must use DECIMAL in the column valor, use varchar and the format is not numerical is able to fail as well.

  • Sorry William, but see, again, my code, what you said is that it doesn’t make sense "The problem is that you are trying to add up after using number_format, it makes no sense".

  • @Gustavosevero only has a Return, the answer explains where its failure, number_format is only necessary after the whole sum, and you are trying to do before, test the example. I edited the code, I had a ; wrong, read where I commented "//Aqui você usa o number_format"

  • 1

    Okay, I put number_format inside $Return and got: $Return = array( 'valueTotalE' => number_format($valueTotalE,2,','.') ); Now it works. I was not understanding you saying that I was putting number_format before the sum kkkk

  • The way you left it in the solution, it doesn’t work because I was doing it wrong.

  • @Gustavosevero "does not work" is extremely vague, does not work what? What happens? You can explain?

  • No added values appear.

  • @Gustavosevero I still do not understand, does not appear or appears wrong. Please explain better.

  • 1

    Forget it, I re-did it like you put it and it worked. I’m sorry.

Show 3 more comments

Browser other questions tagged

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