如何在 .NET 中自定义 Math.Round 的行为?

发布于 2024-11-02 04:26:22 字数 410 浏览 2 评论 0原文

我必须使用遵循以下行为的舍入方法:

7.00 ->圆-> 7
7.50->圆-> 7
7.51 -> 7.51圆-> 8

我尝试使用Math.Round,但它的工作方式有点不同。

Dim val As Decimal = 7.5
Dim a As Decimal = Math.Round(val, 0) ' -> 8
Dim b As Decimal = Math.Round(val, 0, MidpointRounding.AwayFromZero) ' -> 8
Dim c As Decimal = Math.Round(val, 0, MidpointRounding.ToEven) ' -> 8

如何实现我的舍入逻辑?

I have to use a round method that follows this behavior:

7.00 -> round -> 7
7.50 -> round -> 7
7.51 -> round -> 8

I tried to use Math.Round, but it works a little bit different.

Dim val As Decimal = 7.5
Dim a As Decimal = Math.Round(val, 0) ' -> 8
Dim b As Decimal = Math.Round(val, 0, MidpointRounding.AwayFromZero) ' -> 8
Dim c As Decimal = Math.Round(val, 0, MidpointRounding.ToEven) ' -> 8

How can I implement my rounding logic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拧巴小姐 2024-11-09 04:26:22

您可以从总数中减去 0.01,然后调用 Math.round(..)

double d = 7.5;
double result = Math.Round(d - 0.01);

如果数字为负数,您将必须执行以下操作才能获得相同的结果:

double d = -7.5;
if (d < 0)
{
    double tmp = Math.Abs(d) - 0.01;
    double result = -Math.Round(tmp);
}

工作示例此处

但请注意,正如其他几个人指出的那样,这种行为可能不是您想要的。

如果您阅读了此答案的评论,@alex zhevzhik 还指出,如果输入的小数位数超过 2 位,则此解决方案将失败。

You could subtract 0.01 from the total and then call Math.round(..).

double d = 7.5;
double result = Math.Round(d - 0.01);

If the number is negative you will have to do the following to get the same result:

double d = -7.5;
if (d < 0)
{
    double tmp = Math.Abs(d) - 0.01;
    double result = -Math.Round(tmp);
}

Working example here.

Note however that this behaviour is probably not what you want as noted by several others.

If you read the comments of this answer, @alex zhevzhik also noted that this solution will fail if the input would have more than 2 decimals.

梦言归人 2024-11-09 04:26:22

Midpoint 无法提供适当的功能。看一下表中备注中的第一行和第三行。如果将 val 更改为 6.5,您将获得预期的行为,但如果更改为 7.5,则不会。

您应该编写自己的此类舍入实现。

Javed Akram 的实现很好,但它在处理负数时完全错误。
由于您没有提供负数舍入的详细信息,我认为标准舍入适合。此外,您应该考虑“特殊”双精度值:

static class Helper
{
    public static double Round(double val)
    {
        if (Double.IsNaN(val) || Double.IsNegativeInfinity(val) || Double.IsPositiveInfinity(val))
        {
            return val;
        }

        var decimalPart = Math.Truncate(val);
        if (val >= 0)
        {
            if (val - decimalPart <= 0.5)
            {
                return Math.Floor(val);
            }
            else
            {
                return Math.Ceiling(val);
            }
        }
        else
        {
            return Math.Round(val, 0);
        }
    }
}

Midpoint couldn't provide appropriate functionality. Take a look at first and third rows in the table in remarks. If you change your val to 6.5, you will get expected behaviour, but not with 7.5.

You should write your own implementation of such rounding.

Javed Akram's implementation is good, but it works completely wrong with negative numbers.
As you didn't provide details of rounding of negative numbers, I suppose standart rounding suits. In addition you should take into account "special" double values:

static class Helper
{
    public static double Round(double val)
    {
        if (Double.IsNaN(val) || Double.IsNegativeInfinity(val) || Double.IsPositiveInfinity(val))
        {
            return val;
        }

        var decimalPart = Math.Truncate(val);
        if (val >= 0)
        {
            if (val - decimalPart <= 0.5)
            {
                return Math.Floor(val);
            }
            else
            {
                return Math.Ceiling(val);
            }
        }
        else
        {
            return Math.Round(val, 0);
        }
    }
}
堇年纸鸢 2024-11-09 04:26:22

好吧,我不知道是否有 Math.Round 方法可以满足您的要求,但我认为您需要编写自己的方法。因为通常情况下,7.5 会四舍五入为 8,除非我忘记了高中所学的一切。

Well I don't know if there is a Math.Round method that does what you want, but I think you will need to write your own. Because normally, 7.5 would be rounded to 8, unless I forgot everything learned in highschool.

趴在窗边数星星i 2024-11-09 04:26:22
    Dim i As Decimal
    Dim j As Integer
    Dim k As Decimal

    i = 7.51
    k = i - Math.Truncate(i)  'decimal part of number

    If (k <= 0.5) Then
        j = Math.Floor(i)
    Else
        j = Math.Ceiling(i)
    End If
    Dim i As Decimal
    Dim j As Integer
    Dim k As Decimal

    i = 7.51
    k = i - Math.Truncate(i)  'decimal part of number

    If (k <= 0.5) Then
        j = Math.Floor(i)
    Else
        j = Math.Ceiling(i)
    End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文