How to format numerical data extracted from the database for monetary values?

Asked

Viewed 462 times

0

I’m using the library jquery.maskMoney.js to format the system’s monetary data. But when data is saved in the database and returned to the system screen formatting is lost. How to resolve this problem? I would like the formatting to remain, follow some system screens that exemplify this problem.

1. Data with monetary formatting: inserting the data into the forms inserir a descrição da imagem aqui

2. Data without monetary formatting: data being loaded from the database inserir a descrição da imagem aqui
3. Excerpt from the page code edita.blade.php

<div class="col-md-5 form-group">
<label class="control-label">Valor Solicitado (R$)</label>

<input type="text" class="form-control"  id="vlSolicitado" name="vlSolicitado" value="{{str_replace('.',',',$projeto->valor_solicitado)}}" data-thousands="." data-decimal="," data-prefix="R$ " maxlength="18"  required>  
</div>


4. Result of Database Consultation inserir a descrição da imagem aqui

  • 1

    has a function in php: number_format($field['valorprod'], 2, ',', ''), where goes the variable you want to format, the number of boxes after the comma, the separation element ("," or "." ) and the separation of thousands "> 1000,00 (without space) or " > 1 000,00 (with space)

  • Voce can save a way, only using numbers and then when it shows treat the value so that it is shown as Voce you want.

  • To solve the problem, apply the mask on the return of the data after modifying and saving.

2 answers

3

I’ll direct you to process the data before entering it into the database.

  1. I suppose you’re using PHP to process the information before inserting it into the database (Mysql).

  2. I also assume that your database is properly structured to receive information from your forms.

For monetary values the field must be of the type DECIMAL (7,2), two digits after the point and believe that 7 digits before the point is sufficient.

As you are formatting the value with a Jquery mask, as soon as you pass the value to PHP, they also come formatted. Example: 2,000.00.

You need to delete this formatting because your database will not accept the characters: "." and "," thus generating an error.

If form values are being sent via POST, do so:

// Valor recebido do formulário, campo valor solicitado (2.000,00)
$valor = $_POST['valorSolicitado'];

// Removo da variável o "."
$valor = str_replace('.' '', $valor);
// Substituo a "," pelo "." pois é esse formato que o campo do seu banco de dados vai aceitar
$valor = str_replace(',' '.', $valor);

// Esta variável irá imprimir:
// 2000.00

When you recover the value of your data flock, format it using a PHP function called number_format:

// Valor recuperado do banco de dados (2000.00)
$valor = $valor_do_banco_de_dados;

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

// Esta variável irá imprimir:
// 2.000,00
  • Congratulations on the answer, I used the "number_format" function in this case. But, the error remains because the function is not putting the thousand points. And the result was: R$ 2000,00 instead of 2,000,00.

  • Forgive me, I did not put the "." in the function, because I expected the Jquery mask to do this for you. I changed the function by putting the ".". Like this: $value = number_format($value, 2, ',', '.'); I hope it can be useful.

  • Alburqueque, thank you very much, the error has been corrected.

0

Hello @Ruama, try to make a php explode at the value obtained by the bd select, before displaying it in the form. For example:

$sql = "SELECT * FROM TABELA WHERE id=$id_escolhido";
$rs = mysqli_query($con, $sql);

while ($row= mysqli_fetch_array($rs)){
   $valor = $row['valor'];
   $valor_explode = explode(",", $valor);
}
  • 1

    Thank you for the reply.

  • Hi @Ruama worked on your application?

  • did not work I tested with the function "number_format" but the error remains because the function is not putting the thousand points.

  • When you get from the comic, comes in this format "2000.00" this? then you could remove this comma with the explode, transformed the value so "200000". That way when you input it, configured with jquery maskMoney, it will be in the ideal format, "2,000.00"

  • follows the solution: <input type="text" class="form-control" id="vlSolicitado" name="vlSolicitado" value="{number_format($project->valor_solicitado, 2, ',', '.')}}" data-thousands="." data-decimal="," data-prefix="R$ " maxlength="18" required>

Browser other questions tagged

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