Rounding of 5 cents

Asked

Viewed 1,119 times

8

In a PHP system I am developing, I need to update a column of the price table. The price field is in the format float and the administrator informs the percentage of increase.

Example: Informs that you will have a 10% increase.

$valor_antigo = 4,25;

$novo_valor = round(($valor_antigo + ($valor_antigo / $percentual)),2);

Thus, the value after the increase will be 4.68. Setting to leave only two boxes after the comma.

What I need now is to round up these figures by five cents. If I use ceil or `floor, I will have this rounding only 1 in 1.

For example, the value of 4.68 above needs to be saved as 4.70. ` If the value was 5.22, it would be 5.20. 7.46, it would be 7.45.

How can I do that?

  • This is important to fix right away: "The price field is in float format". Mysql has a much better field for this, which is the int. If you want to store the decimal separator next to the number, you have the type decimal also (but if the number of houses is fixed, I still prefer the int).

1 answer

11


Browser other questions tagged

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