2015-02-24 110 views
0

我必须在excel中为我的学校项目进行特殊类型的舍入。如何在excel/vba中进行特殊类型的舍入

如果数量大于#.5,我必须四舍五入。 如果该数字等于#.5,我必须将其四舍五入到最接近的偶数。 如果数量少于#.5,我必须将其舍入。

我希望有人能够帮助我。

Dim getal As Decimal = Nothing 
    Console.WriteLine("voer een nummer in") 
    getal = Console.ReadLine() 

    Dim dec As Integer = Nothing 
    Console.WriteLine("voer het aantal deciale in") 
    dec = Console.ReadLine() 

    Dim factor As Integer = 0 
    Dim floor As Decimal = 0 
    Dim decimaal As Decimal = 0 
    Dim antwoord As Decimal = 0 
    Dim percentage As Decimal = 0 
    Dim GetalTimesFactor As Decimal = 0 
    Dim OnNoukeurigheid As Decimal = 0 
    Dim VerFactor As Integer = 0 
    Dim HasFactor As Boolean = False 
    Dim notation As String = Nothing 
    Dim RoundUp As Decimal = 0 

    If getal > 1000 Then 
     While getal > 10 
      getal = getal/10 
      VerFactor = VerFactor + 1 
      HasFactor = True 
     End While 
     Console.WriteLine("getal: " & getal) 


    End If 

    If getal < 0.0001 Then 
     While getal < 1 
      getal = getal * 10 
      VerFactor = VerFactor - 1 
      HasFactor = True 
     End While 
    End If 

    Select Case dec 
     Case 0 
      factor = 1 
      OnNoukeurigheid = 0.5 
      RoundUp = 1 
     Case 1 
      factor = 10 
      OnNoukeurigheid = 0.05 
      RoundUp = 0.1 
     Case 2 
      factor = 100 
      OnNoukeurigheid = 0.005 
      RoundUp = 0.01 
     Case 3 
      factor = 1000 
      OnNoukeurigheid = 0.0005 
      RoundUp = 0.001 
    End Select 

    GetalTimesFactor = getal * factor 
    floor = Decimal.Floor(GetalTimesFactor) 
    floor = floor/factor 
    decimaal = getal - floor 
    Console.WriteLine("floor: " & floor) 

    If decimaal > OnNoukeurigheid Then 
     floor = floor * factor 
     antwoord = floor + 1 
     antwoord = antwoord/factor 
    ElseIf decimaal = OnNoukeurigheid Then 
     antwoord = Decimal.Round(getal, dec, MidpointRounding.ToEven) 
    Else 
     Console.WriteLine("decimaal: " & decimaal) 
     Console.WriteLine("getal: " & getal) 
     percentage = (decimaal/getal) * 100 
     If percentage < 5 Then 
      Console.WriteLine("percentage is: " & Decimal.Round(percentage, 1) & "%") 
      antwoord = floor 
     Else 
      Console.WriteLine("percentage is: " & Decimal.Round(percentage, 1) & "%") 
      antwoord = floor + RoundUp 
     End If 
    End If 

    If HasFactor Then 
     notation = "E" & Format(VerFactor, "00") 
    End If 

    Console.WriteLine(antwoord & notation) 
    Console.ReadLine() 

这是我在快递取得了它的工作,但它并没有在宏观

注工作:对不起,荷兰varables

+0

是否有是VBA或Excel公式可以工作吗?你有什么尝试?你必须缩小一点,因为它是一个非常广泛的问题。 – 2015-02-24 21:03:17

+0

[Banker's Rounding](http://support.microsoft.com/kb/196652/en-us)可能会引起您的兴趣。 – pnuts 2015-02-24 21:09:37

+0

看到问题比看起来更难。我知道如何绕到最近,甚至我知道如何往上或向下。但困难的部分是检测何时必须这样做。 – 2015-02-24 21:18:39

回答

1

在VBA这是很简单的,因为VBA Round功能可以进行这种舍入:

Function VBARound(N As Double, Optional NumPlaces As Long = 0) As Double 
    VBARound = Round(N, NumPlaces) 
End Function 

NUMPLACES将允许您选择舍入为整数以外的值。

+0

你是一个真正的人。它似乎工作。谢谢。 – 2015-02-24 21:32:04

+0

高兴助阵 – 2015-02-24 21:33:31

0

我有点不确定,如果这就是你要找的人在你的描述,你似乎意味着寻找唯一全数字的结果,所以我离开了第二个变量列入额外的小数点功能保持简单。我想我理解你的四舍五入一点不同于以前的答案,使得.5的情况下,你会选择舍入到整个为准#为偶数(不知道为什么会这样,但如果它是在这里你去! )。

我为我的马虎格式道歉......我教VBA用户自所以有可能是一个更有效的方式:

Function CustomRound(N As Double) 
    If N - Application.WorksheetFunction.RoundDown(N, 0) = 0.5 Then 
     If Application.WorksheetFunction.RoundDown(N, 0) Mod 2 = 0 Then 
     CustomRound = Application.WorksheetFunction.RoundDown(N, 0) 
     Else 
     CustomRound = Application.WorksheetFunction.RoundUp(N, 0) 
     End If  
    Else 
     CustomRound = Round(N, 0) 
    End If 
End Function 
+0

为什么你觉得以前的答案不全面甚至0.5到整个为准#是什么吗? – 2015-02-24 23:10:22