我希望总是在 c # 中四舍五入,例如,从6.88到7,从1.02到2,等等。
我该怎么做?
Use Math.Ceiling: Math.Ceiling(value)
Math.Ceiling(value)
Use Math.Ceiling()
Math.Ceiling()
double result = Math.Ceiling(1.02);
If negative values are present, Math.Round has additional options (in .Net Core 3 or later).
I did a benchmark(.Net 5/release) though and Math.Ceiling() is faster and more efficient.
Math.Round( 6.88, MidpointRounding.ToPositiveInfinity) ==> 7 (~23 clock cycles) Math.Round(-6.88, MidpointRounding.ToPositiveInfinity) ==> -6 (~23 clock cycles) Math.Round( 6.88, MidpointRounding.AwayFromZero) ==> 7 (~23 clock cycles) Math.Round(-6.88, MidpointRounding.AwayFromZero) ==> -7 (~23 clock cycles) Math.Ceiling( 6.88) ==> 7 (~1 clock cycles) Math.Ceiling(-6.88) ==> -6 (~1 clock cycles)