Rounding function to nearest 1/4

asp.net, c#, rounding

Solution

`0.25` is 1/4, so you can get it easily by

double rounded = round(4.0 * hoursEntered) / 4.0

`round` does not exist. You must use appropriate rounding function and options. For aspx.cs (C# codebehind) see `System.Math.Round`.

Also, to link it to the textbox on the webpage, you will need some rule/validator or textchange handler or anywhere around the typical text parsing. You may also try doing it in JavaScript client side, the *4/4 trick stays the same, just `round` function will have different name.

Problem

We only want to auto round the user entered values to the nearest .25 hour interval. So the user can enter 1.55 and it would round to 1.50 when saving to the database. If the user enters 1.90 to would save 2.00 to the database.

Original source