How to convert text field with mask to double

Asked

Viewed 902 times

0

I have a field in the html text type that has a coin mask. I have to save this value in a field of the kind double in the database, but I’m having trouble converting, where the value that would be after the comma is deleted 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 make the conversion to double)

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

Form Screen

inserir a descrição da imagem aqui

String and double printing fabricinserir a descrição da imagem aqui

3 answers

2

The problem occurs when you have a high value where "," and "." appear, for example: 5,150,50

You are just replacing the commas by a point, that is, after your replace the value is this way 5.150.50 Values of double type 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 so:

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

1

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

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

Test this.


You can also use the strtr:

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

Test this.

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


Remember 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 from happening you need to validate the number of . that exist or check whether 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 also do is validate the start input value, at least by checking if there is a comma, if this is required.

0


What’s happening is that the thousand separation point 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;

Browser other questions tagged

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