如何舍入到最接近的0.5?

时间:2009-08-25 16:32:55

标签: c# math function

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

如果数字为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
等等...

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

11 个答案:

答案 0 :(得分:181)

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

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

答案 1 :(得分:59)

乘以2,舍入,然后除以2

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

答案 2 :(得分:14)

以下是我编写的一些方法,它们总是向上或向下舍入到任何值。

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;
    }
}

答案 3 :(得分:1)

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);

答案 4 :(得分:1)

有几种选择。如果考虑性能,请测试它们以查看哪个在大循环中最快。

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;
}

答案 5 :(得分:1)

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

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

答案 6 :(得分:1)

这些代码行将浮动 dx 捕捉到最近的捕捉:

if (snap <= 1f)
    dx = Mathf.Floor(dx) + (Mathf.Round((dx - Mathf.Floor(dx)) * (1f / snap)) * snap);
else
    dx = Mathf.Round(dx / snap) * snap;

因此,如果 snap 为 0.5,则值四舍五入到最接近的 0.5 值(1.37 变为 1.5),如果是 0.02,则值四舍五入到最接近的 0.02((1.37 变为 1.38))。如果 snap 为 3,则值四舍五入到最接近的 3(7.4 到 6,7.6 到 9)等等......我用它来快速捕捉场景中的对象,因为统一默认捕捉对我来说似乎不太好.

答案 7 :(得分:0)

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

答案 8 :(得分:-1)

我也遇到了这个问题。 我主要在Actionscript 3.0中编写代码,它是Adobe Flash Platform的基本编码,但语言中有类似的字体:

我想出的解决方案如下:

//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);

几乎就是这样。 注意使用'Numbers'和'Integers'以及它们的处理方式。

祝你好运!

答案 9 :(得分:-1)

正确的方法是:

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

                return finalPrice;

            }

答案 10 :(得分:-1)

此答案摘自Rosdi Kasim在John Rasch提供的答案中的评论。

约翰的答案有效,但确实有溢出的可能性。

这是我的Rosdi代码版本:

我也将其放在扩展名中以使其易于使用。该扩展不是必需的,可以毫无问题地用作功能。

<Extension>
Public Function ToHalf(value As Decimal) As Decimal
    Dim integerPart = Decimal.Truncate(value)
    Dim fractionPart = value - Decimal.Truncate(integerPart)
    Dim roundedFractionPart = Math.Round(fractionPart * 2, MidpointRounding.AwayFromZero) / 2
    Dim newValue = integerPart + roundedFractionPart
    Return newValue
End Function

用法如下:

Dim newValue = CDec(1.26).ToHalf

这将返回1.5

相关问题