开发者

Silverlight - round doubles away from zero

开发者 https://www.devze.com 2023-01-01 09:28 出处:网络
In Silverlight the Math.Round() method does not contain an overload with \'MidpointRounding\' parameter. Wh开发者_Python百科at is the best approach to round a double away from zero in Silverlight in t

In Silverlight the Math.Round() method does not contain an overload with 'MidpointRounding' parameter. Wh开发者_Python百科at is the best approach to round a double away from zero in Silverlight in this case?

Example:

Math.Round(1.4) => 1

Math.Round(1.5) => 2

Math.Round(1.6) => 2


Any number of "hacks" will do it, for example:

Public Shared Function SpecialRound(ByVal inVal) As Double
    if (inVal < 0)
        Return Math.Ceil(inVal-0.5)
    Return Math.Floor(inVal+0.5)
End Function

I do not know of a "good" way to do it.


public double RoundCorrect(double d, int decimals)
{
    double multiplier = Math.Pow(10, decimals);

    if (d < 0)
        multiplier *= -1;

    return Math.Floor((d * multiplier) + 0.5) / multiplier;

}

For the examples including how to use this as an extension see the post: .NET and Silverlight Rounding

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号