Incompatibility between maskmoney and number_format

1

I have a form field in which I am using maskmoney , but when the number entered enters the thousand, number_format returns only the first four decimal places.

Calling maskmoney :

$(function(){
        $("#Cultsal").maskMoney({symbol:'R$ ',
            showSymbol:true, thousands:'.', decimal:',', symbolStay: true});

The field input :

            <label for="Cultsal">Digite o valor</label>
        <input id="Cultsal" name="Tultsal" type="text" size="10">

The php:

         echo "O valor digitado foi de R$ ". number_format($ultsalbase, 2, ",", ".");

However, when the output is a thousand, the php returns only the first four digits. For example, if you enter 1000, the maskmoney has 1,000.00, but the php returns 10.00. Up to a hundred works normal, type, I enter with 100, maskmoney shows 100.00 and php returns 100.00. It only gives a prolema when it passes into the thousand.

Is there a solution, or do I have to use another mask?

Edit: I need number_format because when I use the value in a function with another variable, it does not "inherit" the formatting properly (it does not include the ", 00", sometimes puts .000 etc). For example:

if ($tiposal=="hora") {
  $rem = ultsalbase * $multipl;
 }

Even though I put the number_format only in the other variable ( $rem in case) it gives the same error.

    
asked by anonymous 15.04.2015 / 18:09

2 answers

1

Gustavo, if the input field is already doing the formatting, why do you want to do this again in PHP? If you want to turn into money to put in the database you must remove the comma from the number and leave the point in place of the comma. Use Str_Replace ().

    
15.04.2015 / 18:24
1

I do not know if it is the most effective way, but I managed to solve it from Diego's tip, as follows:

$ultsalbase = $_POST ["Tultsal"]; // o campo do formulário, que vem com a formatação do maskmoney (1.000,00)
$ultsalbase2 = str_replace('.', '', $ultsalbase); // tira o ponto (fica 1000,00)
$ultsalbase3 = str_replace(',', '.', $ultsalbase2); // muda a vírgula para ponto (fica 1000.00

echo number_format($ultsalbase3, 2, ",", "."); // e a saída é correta, 1.000,00. :-)
    
16.04.2015 / 23:24