VB는 거의 사용하지 않는데,
문의가 와서 확인을 해보니 아래와 같이 Val 함수 버그가 있었습니다.
즉 이러한 문제를 피하기 위해 하는 수 없이 함수를 만들어 사용해야만 합니다.
---
Val 에러를 방지하는 방법
Many programmers use VB's Val() function to convert user inputted
strings into numbers. This useful function returns zero (0) for
strings that are meaningless as numeric input. Unfortunately, Val()
has a bug in its routines. Notably, Val("6%") returns 6, Val("6.%")
returns 6, Val("6.0%") returns 6, but Val("6.1%") generates a
Type Mismatch error! Hence, for a solid program, no cautious VB
programmer can use Val() as-is for generic inputs.
To avoid this error, try a routine that replaces the offending
percentage sign with some other character, as in this:
문의가 와서 확인을 해보니 아래와 같이 Val 함수 버그가 있었습니다.
Val("5D1")결과값을 5로 기대하겠지만 Val 함수는 50을 리턴합니다.
즉 이러한 문제를 피하기 위해 하는 수 없이 함수를 만들어 사용해야만 합니다.
Function myInt(s As String) As Integer For i = 1 To Len(s) If IsAlpha(Mid(s, i, 1)) Then Exit For End If Next If i > 1 Then s = Mid(s, 1, i - 1) End If myInt = Val(s) End Function Function IsAlpha(s As String) As Boolean Dim i As Integer Dim ch As Byte IsAlpha = True For i = 1 To Len(s) ch = Asc(UCase$(Mid(s, i, 1))) If Not ((ch >= 65) And (ch <= 90)) Then IsAlpha = False Exit For End If Next i End Function
---
Val 에러를 방지하는 방법
Many programmers use VB's Val() function to convert user inputted
strings into numbers. This useful function returns zero (0) for
strings that are meaningless as numeric input. Unfortunately, Val()
has a bug in its routines. Notably, Val("6%") returns 6, Val("6.%")
returns 6, Val("6.0%") returns 6, but Val("6.1%") generates a
Type Mismatch error! Hence, for a solid program, no cautious VB
programmer can use Val() as-is for generic inputs.
To avoid this error, try a routine that replaces the offending
percentage sign with some other character, as in this:
TheString = "15.7%" Mid(TheString, Instr(TheString, "%"),1) = "$" Debug.Print Val(TheString)
댓글
댓글 쓰기