Php formatting of numerics to save to mysql database as DECIMAL

Asked

Viewed 698 times

0

I am implementing Ckeckout Cielo and it is always returned numerical values for prices.

Monetary values are always treated as integer values, without representation of decimal places, and the last two digits are considered as cents.

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

I think about using the function of substr_replace to add (.) before the last two houses, as in the following example::

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

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

In this context, apply the substr_replace for recording decibel values in the database Mysql would be suitable for any monetary value coming from Cielo?

  • divided by 100 !!!

  • have to find out if this is the way it is, as in the example 128687 if formatting no correct is split by 100, if the number has decimal places the bank will record if not also, but what would be nice is to check with CIELO or manual if it is like that.

  • @Virgilio Novic the first example was taken in full from the Cielo manual.

  • then do the division by 100! the value could already be saved in the base!

1 answer

1


In that case the CIELO reported, would indicate the division by 100 and would abandon the idea to use substr_replace, because:

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

Code

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

or worst still values with a home:

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

By dividing the value by 100 the value would be formatted and suitable for recording in 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 tell the division solves your problem.

  • 1

    I liked the solution, but in the case CIELO does not return values of one house only. 1,00 returns 100 and 20,00 returns 2000. I will use the division, thank you.

Browser other questions tagged

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