-2

在 Windows 7 上使用 VB6 应用程序,这两行都返回 TRUE,因为不考虑小数点分隔符:

IsNumeric("123.45")
IsNumeric("123,45")

在 Windows 8 或 Windows 2012 上,相同的代码会根据区域设置返回 TRUE 或 FALSE。考虑逗号作为区域设置中定义的小数分隔符,则:

IsNumeric("123.45") returns FALSE
IsNumeric("123,45") returns TRUE

有没有办法在不重新编译应用程序的情况下恢复“旧”行为?

4

1 回答 1

3

不是您使用的 Windows 版本的新问题。它始终基于机器的区域设置。

我为我的应用程序所做的是创建了自己的函数:

Public Function IsNumber(ByRef Expression As Variant) As Boolean
    Select Case VarType(Expression)
    Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbDate, vbBoolean, vbDecimal, vbByte
        IsNumber = True
    Case vbString
        Dim Negative As Boolean
        Dim Number As Boolean
        Dim Period As Boolean
        Dim Positive As Boolean
        Dim X As Long
        For X = 1& To Len(Expression)
            Select Case Mid$(Expression, X, 1&)
            Case "0" To "9"
                Number = True
            Case "-"
                If Period Or Number Or Negative Or Positive Then Exit Function
                Negative = True
            Case "."
                If Period Or Exponent Then Exit Function
                Period = True
            Case "E", "e"
                If Not Number Then Exit Function
                If Exponent Then Exit Function
                Exponent = True
                Number = False
                Negative = False
                Period = False
            Case "+"
                If Not Exponent Then Exit Function
                If Number Or Negative Or Positive Then Exit Function
                Positive = True
            Case vbSpace, vbTab, vbVerticalTab, vbCr, vbLf, vbFormFeed
                If Period Or Number Or Exponent Or Negative Then Exit Function
            Case Else
                Exit Function
            End Select
        Next X
        IsNumber = Number
    End Select
End Function

如果您明确地处理字符串,那么您可以简化该功能。而且您可能不想处理指数,所以这可能更适合:

Public Function IsNumber(ByRef Expression As String) As Boolean
    Dim Negative As Boolean
    Dim Number As Boolean
    Dim Period As Boolean
    Dim X As Long
    For X = 1& To Len(Expression)
        Select Case Mid$(Expression, X, 1&)
        Case "0" To "9"
            Number = True
        Case "-"
            If Period Or Number Or Negative Then Exit Function
            Negative = True
        Case "."
            If Period Then Exit Function
            Period = True
        Case vbSpace, vbTab, vbVerticalTab, vbCr, vbLf, vbFormFeed
            If Period Or Number Or Negative Then Exit Function
        Case Else
            Exit Function
        End Select
    Next X
    IsNumber = Number
End Function

当您需要转换数字时,请使用Str()而不是CLng()/ CInt()/ CDbl()/ CSng()/ Val()Str()无论语言环境如何,都将句点视为小数,就像我IsNumber()上面的函数一样。

于 2013-08-21T22:28:53.383 回答