Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round decimal value up to nearest 0.05 value?

Tags:

Is there any way to round up decimal value to its nearest 0.05 value in .Net?

Ex:

7.125 -> 7.15

6.66 -> 6.7

If its now available can anyone provide me the algo?

like image 219
Prashant Cholachagudda Avatar asked Sep 19 '09 12:09

Prashant Cholachagudda


People also ask

How do I round up to the nearest 0.05 in Excel?

Select cell A2 in the worksheet to enter this cell reference as the number argument. Select the Multiple line. Type in 0.05 so that the number in cell A2 will be rounded up or down to the nearest multiple of 5 cents. Select Done to return to the worksheet.

Is .05 rounded up or down?

Whenever the value right after the decimal is less than 5, we round down; otherwise, we round up.


2 Answers

How about:

Math.Ceiling(myValue * 20) / 20 
like image 62
Adam Bellaire Avatar answered Sep 28 '22 22:09

Adam Bellaire


Use this:

Math.Round(mydecimal / 0.05m, 0) * 0.05m; 

The same logic can be used in T-SQL:

ROUND(@mydecimal / 0.05, 0) * 0.05 

I prefer this approach to the selected answer simply because you can directly see the precision used.

like image 31
Marc Avatar answered Sep 28 '22 23:09

Marc