-2

I want to round any numbers on the basis of the following numbers. The numbers are as follows. Assume,

case 1) I have number 0.1 Between 0.24, then the round output should be 0.25
case 2) number 0.26 Between 0.49, then the round output should be 0.50
case 3) number 0.51 Between 0.74, then the round output should be 0.75
case 4) number 0.76 Between 0.99, then the round output should be 1.00

case 5) I have number 1.1 Between 1.24, then the round output should be 1.25
case 6) number 1.26 Between 1.49, then the round output should be 1.50
case 7) number 1.51 Between 1.74, then the round output should be 1.75
case 8) number 1.76 Between 1.99, then the round output should be 2.00

Here the numbers come in the dynamic. and the number will always be bigger than zero.

Please help me and the suggestion of how it might be possible.

sanjay sisodiya
  • 455
  • 4
  • 14

1 Answers1

0

Looks like (American) money. Looks like "round up to the nearest quarter".

Notice the pattern. In every case it should add some number to get the nearest quarter. All you need do is calculate how much to add.

You're probably familiar with the modulo opearator, and the "technical" description:

$a % $b     Modulo  Remainder of $a divided by $b.

But remember, the "remainder" also tells the "distance" between the divisor ($b) and a multiple of the dividend ($a).

A simple example: 27 % 25 = 2. 27 is 2 more than a multiple of 25. It is also 25 - 2 = 23 less than the next multiple of 25. To round up, then, the equation would be 27 + (25 - (27 % 25))

But alas, % only works for integers. You would need to use the fmod function to operate on floats.

(WARNING: none of the examples is a multiple of .25. Those inputs would have to be handled with an exception case to prevent adding an extra .25).

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15