Incompatibility between maskmoney and number_format

Asked

Viewed 433 times

1

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

The call of 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">

php:

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

However, when the output is miles, php returns only the first four digits. For example, if I enter 1000, maskmoney presents 1,000.00, but php returns 10.00. Up to 100 works normal, like, I enter 100, the maskmoney shows 100.00 and php returns 100.00. Only gives prolema when goes to thousand.

There is a solution, or I’ll have to use another mask?

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

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

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

2 answers

1

I don’t know if this is the most effective way, but I was able 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. :-)

1


Gustavo, if the input field is already doing the formatting, why do you want to do it again in PHP ? If you want to turn into money to put in the database you must take the comma out of the number and leave the dot in place of the comma. Use the Str_replace().

  • Because when I use the value in another variable, it doesn’t include the ".00" in the output (I’ll edit the question about it), so I started using number_format to format the output. What I wanted was for maskmoney to just show the number with the formatting for the user in the field, and then formatting the output).

  • I get it. I usually use.Js http://numeraljs.com/

  • Thanks, I’ll try it here.

  • Dude, your tip to include in the database worked also for what I wanted! I’ll post a reply with the solution. Thanks again! Hug.

Browser other questions tagged

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