各位好!小弟有个问题请教:以下要求VB语句如何写?如果同时能写在一个语句里最好,分条件也可以,但这个条件不知如何写了。感谢!!!!!1.小数位在0.5以上,则进到下一个整数位.    例如:8.7,则返回为9
2.小数位大于0.1,小于0.5的,则返回为0.5,    例如:1.4条,则返回为1.5
3.小数位为0.1及以下,则省掉小数位,即取整。  例如:3.06条,则返回为3

解决方案 »

  1.   

    Public Function RoundOff(ByVal x As Double) As Double
        If x - Int(x) < 0.1 Then
            RoundOff = Int(x)
        Else
            RoundOff = Int((x + 0.5) * 2) / 2
        End If
    End FunctionPrivate Sub Command1_Click()    Text2 = RoundOff(Val(Text1))
        
    End Sub
      

  2.   

    楼主没说清楚边界情况,按照通常均等分段的做法,假定
    区间 [0.1, 0.6) -> 0.5
    区间 [0.6, 1.1) -> 1.0
    Option ExplicitSub Main()
        Dim i As Long
        Dim v As Double
        
        For i = 0 To 210
            Select Case (i Mod 10)
                Case 0, 1, 9
                    v = i / 100
                    Debug.Print Format(v, "0.00"), Format(MyRound(v), "0.0#")
            End Select
        Next
        
    End SubFunction MyRound(ByVal value As Double) As Double
        MyRound = Int((value + 0.4) * 2) / 2
    End Function
      

  3.   


    Private Sub Command1_Click()
        Dim sngP As Single
        Dim strP As String
        Dim intP As Integer
        Dim intN As Integer
        sngP = 8.05
        strP = CStr(sngP)
        intP = InStr(1, strP, ".", vbTextCompare)
        If intP <= 0 Then Exit Sub
        strP = Mid(strP, intP + 1, 1)
        intP = CInt(strP)
        If intP > 1 And intP <= 5 Then
            sngP = Fix(sngP) + 0.5
        ElseIf intP > 5 Then
            sngP = Fix(sngP + 0.5)
        Else
            sngP = Fix(sngP)
        End If
        Debug.Print Format(sngP, "#0.0")
    End Sub