-1

I am dealing with money and would love to round the numbers to the lowest payable amount on market. Let say I have a calculation resulting in:

  • 25,467.56
  • 5333.65
  • 532
  • 60
  • 54

In the market the lowest amount payable is 50, so somehow these numbers have to be rounded to the nearest 50 and the result displayed should be:

  • 25,450
  • 5300
  • 500
  • 50
  • 50

How do accurately and reliably do that? Does PHP offer a built in function to handle that? I tried to Google and get something to read but I got wrong answers so I must be using wrong keyword. Any pointer to the right direction is appreciated!

Note: I use moneyphp for financial calculation and this is a user feature so it is okay to loose that precision.

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
  • 3
    divide by 50, round down, multiply by 50. – tkausl Mar 05 '23 at 17:41
  • can't believe it is that easy! Thank you. – Stefano Mtangoo Mar 05 '23 at 18:33
  • 1
    The desired results 5300 and 500 shown for inputs of 5333.65 and 532 do not match the heading, nor do they match the accepted solution. Please correct your examples. – jspit Mar 05 '23 at 18:49
  • Please feel free to edit the title to match the whole body. Am not good at that and that help is appreciated. The body reflects what I want. I have put a comment that accepted answer does not indeed answer exactly what I wanted but points me to the right direction. If you can put a better answer, I will gladly accept it! – Stefano Mtangoo Mar 07 '23 at 06:38

1 Answers1

1

PHP has an built in round function. Using this and some calculations to round to 50 instead of 1 you get the code:

echo(round(x/50)*50);

Here x is the number it rounds. This works by first getting how many multiples of 50 x is, rounding that and then multiplying it by 50 again to get the result.

Wijze
  • 94
  • 5