C# 如何四舍五入到最接近的 0.5?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1329426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:12:23  来源:igfitidea点击:

How do I round to the nearest 0.5?

c#mathfunction

提问by Murtaza Mandvi

I have to display ratings and for that i need increments as follows:

我必须显示评级,为此我需要如下增量:

If the number is 1.0 it should be equal to 1
If the number is 1.1 should be equal to 1
If the number is 1.2 should be equal to 1
If the number is 1.3 should be equal to 1.5
If the number is 1.4 should be equal to 1.5
If the number is 1.5 should be equal to 1.5
If the number is 1.6 should be equal to 1.5
If the number is 1.7 should be equal to 1.5
If the number is 1.8 should be equal to 2.0
If the number is 1.9 should be equal to 2.0
If the number is 2.0 should be equal to 2.0
If the number is 2.1 should be equal to 2.0
and so on...

如果数字是 1.0 应该等于 1
如果数字是 1.1 应该等于 1
如果数字是 1.2 应该等于 1
如果数字是 1.3 应该等于 1.5
如果数字是 1.4 应该等于1.5
如果数字是1.5 应该等于1.5
如果数字是1.6 应该等于1.5
如果数字是1.7 应该等于1.5
如果数字是1.8 应该等于2.0
如果数字是1.9 应该等于2.0
如果数字是 2.0 应该等于 2.0
如果数字是 2.1 应该等于 2.0
等等...

Is there a simple way to compute the required values?

有没有一种简单的方法来计算所需的值?

采纳答案by John Rasch

Multiply your rating by 2, then round using Math.Round(rating, MidpointRounding.AwayFromZero), then divide that value by 2.

将您的评分乘以 2,然后使用 舍入Math.Round(rating, MidpointRounding.AwayFromZero),然后将该值除以 2。

Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2

Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2

回答by Neil N

Multiply by 2, round, then divide by 2

乘以 2,取整,然后除以 2

if you want nearest quarter, multiply by 4, divide by 4, etc

如果你想要最近的四分之一,乘以 4,除以 4,等等

回答by Akash Kava

decimal d = // your number..

decimal t = d - Math.Floor(d);
if(t >= 0.3d && t <= 0.7d)
{
    return Math.Floor(d) + 0.5d;
}
else if(t>0.7d)
    return Math.Ceil(d);
return Math.Floor(d);

回答by John Fisher

There are several options. If performance is a concern, test them to see which works fastest in a large loop.

有几种选择。如果性能是一个问题,请测试它们以查看哪个在大循环中运行最快。

double Adjust(double input)
{
    double whole = Math.Truncate(input);
    double remainder = input - whole;
    if (remainder < 0.3)
    {
        remainder = 0;
    }
    else if (remainder < 0.8)
    {
        remainder = 0.5;
    }
    else
    {
        remainder = 1;
    }
    return whole + remainder;
}

回答by Paul Brinkley

Sounds like you need to round to the nearest 0.5. I see no version of roundin the C# API that does this (one version takes a number of decimal digits to round to, which isn't the same thing).

听起来您需要四舍五入到最接近的 0.5。我round在 C# API 中没有看到执行此操作的版本(一个版本需要一些十进制数字来四舍五入,这不是一回事)。

Assuming you only have to deal with integer numbers of tenths, it's sufficient to calculate round (num * 2) / 2. If you're using arbitrarily precise decimals, it gets trickier. Let's hope you don't.

假设您只需要处理十分之一的整数,计算round (num * 2) / 2. 如果您使用任意精确的小数,它会变得更加棘手。让我们希望你不要。

回答by Jason Henn

I had difficulty with this problem as well. I code mainly in Actionscript 3.0 which is base coding for the Adobe Flash Platform, but there are simularities in the Languages:

我也遇到了这个问题。我主要使用 Actionscript 3.0 进行编码,这是 Adob​​e Flash 平台的基本编码,但语言有相似之处:

The solution I came up with is the following:

我想出的解决方案如下:

//Code for Rounding to the nearest 0.05
var r:Number = Math.random() * 10;  // NUMBER - Input Your Number here
var n:int = r * 10;   // INTEGER - Shift Decimal 2 places to right
var f:int = Math.round(r * 10 - n) * 5;// INTEGER - Test 1 or 0 then convert to 5
var d:Number = (n + (f / 10)) / 10; //  NUMBER - Re-assemble the number

trace("ORG No: " + r);
trace("NEW No: " + d);

Thats pretty much it. Note the use of 'Numbers' and 'Integers' and the way they are processed.

差不多就是这样。请注意“数字”和“整数”的使用以及它们的处理方式。

Good Luck!

祝你好运!

回答by NER1808

Here are a couple of methods I wrote that will always round up or down to any value.

这里有几个我写的方法,它们总是向上或向下舍入到任何值。

public static Double RoundUpToNearest(Double passednumber, Double roundto)
{
    // 105.5 up to nearest 1 = 106
    // 105.5 up to nearest 10 = 110
    // 105.5 up to nearest 7 = 112
    // 105.5 up to nearest 100 = 200
    // 105.5 up to nearest 0.2 = 105.6
    // 105.5 up to nearest 0.3 = 105.6

    //if no rounto then just pass original number back
    if (roundto == 0)
    {
        return passednumber;
    }
    else
    {
        return Math.Ceiling(passednumber / roundto) * roundto;
    }
}

public static Double RoundDownToNearest(Double passednumber, Double roundto)
{
    // 105.5 down to nearest 1 = 105
    // 105.5 down to nearest 10 = 100
    // 105.5 down to nearest 7 = 105
    // 105.5 down to nearest 100 = 100
    // 105.5 down to nearest 0.2 = 105.4
    // 105.5 down to nearest 0.3 = 105.3

    //if no rounto then just pass original number back
    if (roundto == 0)
    {
        return passednumber;
    }
    else
    {
        return Math.Floor(passednumber / roundto) * roundto;
    }
}

回答by Carca

The Correct way to do this is:

正确的做法是:

  public static Decimal GetPrice(Decimal price)
            {
                var DecPrice = price / 50;
                var roundedPrice = Math.Round(DecPrice, MidpointRounding.AwayFromZero);
                var finalPrice = roundedPrice * 50;

                return finalPrice;

            }

回答by user2260011

Public Function Round(ByVal text As TextBox) As Integer
    Dim r As String = Nothing
    If text.TextLength > 3 Then
        Dim Last3 As String = (text.Text.Substring(text.Text.Length - 3))
        If Last3.Substring(0, 1) = "." Then
            Dim dimcalvalue As String = Last3.Substring(Last3.Length - 2)
            If Val(dimcalvalue) >= 50 Then
                text.Text = Val(text.Text) - Val(Last3)
                text.Text = Val(text.Text) + 1
            ElseIf Val(dimcalvalue) < 50 Then
                text.Text = Val(text.Text) - Val(Last3)
            End If
        End If
    End If
    Return r
End Function