2013-10-29 3 views
0

문자열을 int로 캐스팅하려고하는데 어떤 이유로 다음 코드가 작동하는 것 같습니다.문자열을 Int32로 캐스팅 오류

Dim theInt As Int32 = CInt("55e5") 
Console.WriteLine("String to Int32: " & theInt) 

나는 문제가 제대로 변환 이유를 이해하고 출력을 데 5,500,000

+0

원하는 것은 맞습니까? – dbasnett

+0

예, 문자열이 과학 표기법으로 포맷되었음을 인식하지 못했습니다. – GER

+0

나는 분명하다. 나는 550000을 원하지 않았다. 예외를 잡아 사용자에게 입력 오류에 대해 경고 할 것으로 예상했다. – GER

답변

1

그것의 변환이 과학적 표기법에 e5 (즉, 적절한 용어라고 생각?)의 5 배 이상 소수점 자리를 밀어 있도록, 따라서 5500000 (추가 5 초)

+0

좋은 캐치! 감사 – GER

0

답변으로 55 세가 예상되는 곳은 어디입니까? 이전 VB VAL()은이를 리턴합니다.

나는 코드에 대한 .Net Val()을 사용하여 연주했습니다. 대부분 달러 기호, 쉼표()를 사용하지만 확장 될 수 있습니다.

Public Function ValTest(ByVal value As String) As Double 
    If String.IsNullOrEmpty(value) Then Return 0 
    If IsNumeric(value) Then Return CDbl(value.Trim) ' IsNumeric and CDbl strip currency $ and comma, and support accounting negation e.g. ($23.23) = -23.23 
    ' deal with case where leading/trailing non-numerics are present/expected 
    Dim result As String = String.Empty 
    Dim s As String = value.Trim 
    If s.StartsWith("(") Then s.Replace("(", "-") ' leading (= negative number from accounting - not supported by VB.Val() 
    For Each c As Char In s 
     If Char.IsNumber(c) OrElse "-.".Contains(c) Then 
      result = (result + c) 
     End If 
    Next c 
    If String.IsNullOrEmpty(result) Then 
     Return 0 
    Else 
     Return CDbl(result) 
    End If 
End Function