by Viper
12. June 2009 08:31
While working on a clock synchronization service, I had to dig up an old piece of code that I used to round off numbers. For this application I need to round the number to 0 or next 0.5 depending on it proximity to edge. For example if number is 8.001, it gets rounded off to 8.0 and if it is 8.6123 it will be rounded to 8.5. You can argue why not 9 instead of 8.5. Well the logic of the application demands that number is to be rounded down. Here is simple piece of code that will do the trick.
double roundDownNumber = Math.Round(actualNumber * 2.0, 0) / 2.0;
Just simple idea that multiply the number by 2 and round it to ZERO decimal places and then divide by 2.