Formatting with php of numeric to save in the mysql database as DECIMAL

1

I'm implementing Ckeckout Cielo, and numeric values are always returned for prices.

Monetary values are always treated as integer values, without representation of decimal places, with the last two digits being regarded as the cents.

Exemplo: R$ 1.286,87 é representado como 128687 e R$ 1,00 é representado como 100

I'm thinking of using the substr_replace function to add (.) before the last two houses, as in the example below:

substr_replace(100000, '.', -2,0) = 1000.00

substr_replace(100, '.', -2,0) = 1.00

In this context, would it apply to substr_replace for writing decimal values in the Mysql database would be suitable for any money value from heaven?

    
asked by anonymous 18.10.2016 / 15:58

1 answer

1

In this case of CIELO reported, would indicate the division by 100 and would abandon the idea / strong> to use substr_replace , because:

Values below 100 , in case 20 could bring a die like this:

Code

echo substr_replace(20, '.', -2,0);
//saída .20

or worse yet values with a house:

echo substr_replace(2, '.', -2,0);
//saída .2 no qual o correto deveria ser 0.02

By dividing the value by% w / o%, the value would be formatted and suitable for writing to the bank:

echo 20 / 100;
//saída 0.2
echo 2 / 100;
//saída 0.02
echo 128687 / 100;
//saída 1286.87

As far as I can see the division solves your problem.

    
18.10.2016 / 16:31