How to convert text field with mask to double

0

I have a field in html of type text that has a currency mask. I have to save this value to a field of type double in the database, but I'm having trouble converting, where the value that would be after the comma is suppressed after the conversion.

HTML code:

<div class="form-group ">
    <label class="control-label " for="adiantamento">
            Adiantamento
    </label>
    <div class="input-group">
            <div class="input-group-addon">
                R$
            </div>
            <input class="form-control" type="text" id="adiantamento" name="adiantamento" onKeyPress="return(MascaraMoeda(this,'.',',',event))"/>

    </div>
</div>

PHP code (I replace the comma with the dot and try to convert to double)

 $adiantamento=str_replace(',','.',$_POST['adiantamento']);
 $adiantamento_C=(double)$adiantamento;

Form Screen

Stringanddoubleprint

    
asked by anonymous 02.06.2017 / 20:23

3 answers

0

What is happening is that the separating point of thousands is being interpreted as decimal separator, just take it out before replacing the comma:

$adiantamento = str_replace('.', '', $_POST['adiantamento']);
$adiantamento = str_replace(',', '.', $adiantamento);
$adiantamento_C = (double)$adiantamento;
    
02.06.2017 / 20:31
2

The problem happens when you have a high value where the "," and the "." such as: 5,150.50

You are just replacing the commas by a point, that is, after your replace the value is thus 5.150.50 Values of type double have to have only one point, so before you replace the commas by point you should remove the points.

$adiantamento = str_replace( '.' , '', $_POST['adiantamento'] ); // Removeu todos os pontos
$adiantamento = str_replace( ',', '.', $adiantamento); // Substitui todas as virgulas por ponto.

If you want to save 1 line of code you can also do this:

$adiantamento = str_replace( ',', '.', str_replace( '.' , '', $_POST['adiantamento'] ) );
    
02.06.2017 / 20:31
1

An alternative would be to use a single str_replace , no need to use this twice .

echo str_replace(['.', ','], ['', '.'], $valor);

Try This.

You can also use strtr :

echo strtr($valor, ['.' => '', ',' => '.']);

Test this.

The idea is the same as the previous answers, remove the . and then convert the , to . .

Note that if the input is badly formatted, the above functions will not solve, considering:

 $valor = '1,234,56';

This will convert to:

 1.234.56

Which in the end will result in:

 1.234

To prevent this occurring, you must validate the number of . that exists or check that the value in double is equal.

For example:

$valor = '1,234,56'; // Inválido

$valor = strtr($valor, ['.' => '', ',' => '.']); // = 1.234.56

if(strcmp((double)$valor, $valor) !== 0){
    echo 'Erro'; // = Cairá aqui.
}

This will occur because 1.234.56 is different from 1.234 , its value as double.

One thing you can do is also validate the start input value, at least checking for a comma if this is required.

    
02.06.2017 / 20:54