Rounding to nearest fraction (half, quarter, etc.)

后端 未结 4 1411
北荒
北荒 2020-11-28 06:43

So, I need to create the following functions but my head can\'t think of any possibility in PHP without complicated math.

  • Round always up to t
4条回答
  •  情话喂你
    2020-11-28 07:09

    Please note that the answer isn't really water tight. Since we're dealing with floats here it's not guaranteed that when you divide the rounded number by the denominator it returns a neatly round number. It may return 1.499999999999 instead of 1.5. It's the nature of floating point numbers.

    Another round is needed before returning the number from the function.

    Just in case someone lands here from google like I did :)

提交回复
热议问题