问题
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?
回答1:
How about:
Math.Ceiling(myValue * 20) / 20
回答2:
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.
回答3:
Something like this should work for any step, not just 0.05:
private decimal RoundUp (decimal value, decimal step)
{
var multiplicand = Math.Ceiling (value / step);
return step * multiplicand;
}
回答4:
Math..::.Round Method (Decimal, Int32, MidpointRounding)
Rounds a double-precision floating-point value to the specified number of fractional digits. A parameter specifies how to round the value if it is midway between two other numbers.
Math.Round(1.489,2,MidpointRounding.AwayFromZero)
回答5:
Duplicated here and here for ruby and python. It shouldn't be too different.
来源:https://stackoverflow.com/questions/1448458/how-to-round-decimal-value-up-to-nearest-0-05-value