cancel
Showing results for 
Search instead for 
Did you mean: 
WarrenBelz

Round to the nearest 0.5

It is easy enough to round (nearest, up or down) to a specified number of decimal places, but what if you want to round to the nearest half? This is not something "standard" in functions provided, so a combination of other things are required.

Firstly, the code

With(
   {
      wVal: YourValue,
      wInt: 
      RoundDown(
         YourValue,
         0
      )
   },
   wInt + 
   Round(
      Mod(
         wVal,
         wInt
      ) / 5,
      1
   ) * 5
)

To explain how it works

  • The two values at the top are only to avoid repetitive typing. The first wVal is simply the value being rounded and the second wInt is the "Whole number" part of it.
  • We firstly take the whole number then
  • Get the decimal part by taking the Mod (remainder) of dividing the wVal by wInt
  • This is then divided by 5 and rounded to the nearest single decimal place,
  • This produces one of three results
    • 0 for under .25
    • 0.1 for under 0.75
    • 0.2 for over this.
  • This is then multiplied by 5 to produce 0, 0.5 or 1 (which rounds up to the next number).