PHP Commercial Round Currency

Dear Stackoverflowers,

I would like to know what solutions you can solve for the following problem:

This is what I have:

13.90 5.03 7.06 2.51 

This is what I want:

 13.90 5.05 7.05 2.50 

Basically: I want to combine the currency on a commercial basis. The last decimal place can only be rounded to 5 or 10 (adding one to the first digit) or rounded to 5 or 0.

+4
source share
2 answers

The general formula for rounding to the nearest x :

 round(input / x) * x 

And an example of your use case:

 round(5.03 / .05) * .05 = round(100.6) * .05 = 101 * .05 = 5.05 
+9
source
 <?php $int = 5.03; $int *= 20; $int = ceil($int); $int /= 20; echo $int; 

You just need to determine the rounding resolution by multiplying the number (and then by dividing it again). This is a simple math problem.

+5
source

Source: https://habr.com/ru/post/1403735/


All Articles