2013-09-24 90 views
6

我需要能夠判斷整數是整數還是小數。所以13是整數,23.23是小數。VB2010如何判斷一個數是否是整數

所以像;

If 13 is a whole number then 
msgbox("It's a whole number, with no decimals!") 
else 
msgbox("It has a decimal.") 
end if 
+0

什麼類型是您使用包含您的號碼的變量,它是一個字符串? –

回答

7

您可以檢查號碼的樓層和天花板是否相同。如果它相等,那麼它是一個整數,否則它將不同。

If Math.Floor(value) = Math.Ceiling(value) Then 
... 
Else 
... 
End If 
4

的事實,你有一個類型,你都需要確定它是否是一個整數或其他類型的我假設數字符串中包含的判斷。如果是這樣,您可以使用Integer.TryParse方法來確定該值是否爲整數,如果成功,它也會將其作爲整數輸出。如果這不是你正在做的事情,請用更多的信息更新你的問題。

Dim number As String = 34.68 
Dim output As Integer 
If (Integer.TryParse(number, output)) Then 
    MsgBox("is an integer") 
Else 
    MsgBox("is not an integer") 
End If 

編輯:

你可以,如果你使用的是十進制或其他類型的包含您的電話號碼,正像這樣用同樣的想法。

Option Strict On 
Module Module1 

    Sub Main() 

     Dim number As Decimal = 34 
     If IsInteger(number) Then 
      MsgBox("is an integer") 
     Else 
      MsgBox("is not an integer") 
     End If 
     If IsInteger("34.62") Then 
      MsgBox("is an integer") 
     Else 
      MsgBox("is not an integer") 
     End If 

    End Sub 

    Public Function IsInteger(value As Object) As Boolean 
     Dim output As Integer ' I am not using this by intent it is needed by the TryParse Method 
     If (Integer.TryParse(value.ToString(), output)) Then 
      Return True 
     Else 
      Return False 
     End If 
    End Function 
End Module 
+0

對不起,我更新了原帖。輸入始終是一個整數,但我需要找出它是整數還是整數有小數位。 – user2691270

+2

@ user2691270一個[Integer](http://www.techterms.com/definition/integer)不能有小數點,如果有一個小數點,它可以是小數點,雙精度,字符串或單精度或沿着這些直線的東西。這個數字是從哪裏產生的,如果一個文本框可能是一個字符串。 –

+2

@MarkHall:如果您對結果不感興趣,則不需要TryParse的「助手」變量。你可以傳遞一個常量(比如0,-1,42或者其他)或者「nothing」(對於一個整數默認爲0)。 作爲旁註:因爲OP只指定「整數」,所以我建議使用BigInteger.TryParse。 – igrimpe

17
If x = Int(x) Then 
    'x is an Integer!' 
Else 
    'x is not an Integer!' 
End If 
+0

甜而簡單。 – Wakka02

+0

當x是一個字符串時會發生什麼?即「2」,「2.2」,「3.14」?或者更好的是,當x大於21億時會發生什麼?整數(即表ID)可以並超過您可以存儲在Int(Int @@ @ 21億)中的最大值。 – user3541092

+0

我假設嚴格打字。如果數字對於變量來說太大,則會發生溢出異常。 – SSS

0
 
Dim Num As String = "54.54" 
If Num.Contains(".") Then MsgBox("Decimal") 
'Do Something 
+2

一些細節和解釋會提高這個答案的質量。 –

+0

這個答案是唯一一個檢查數字(作爲字符串)格式是否正確的答案。其他答案等於1到1.00,這是錯誤的。 –

1

我假設你的初始值是一個字符串。

,首先檢查字符串值是否爲數字。
,比較數字的地板和天花板。如果它是一樣的,你有一個整數。

我更喜歡使用擴展方法。

''' <summary> 
''' Is Numeric 
''' </summary> 
''' <param name="p_string"></param> 
''' <returns></returns> 
''' <remarks></remarks> 
<Extension()> 
Public Function IsNumeric(ByVal p_string As String) As Boolean 
    If Decimal.TryParse(p_string, Nothing) Then Return True 
    Return False 
End Function 

''' <summary> 
''' Is Integer 
''' </summary> 
''' <param name="p_stringValue"></param> 
''' <returns></returns> 
<Extension()> 
Public Function IsInteger(p_stringValue As String) As Boolean 
    If Not IsNumeric(p_stringValue) Then Return False 
    If Math.Floor(CDec(p_stringValue)) = Math.Ceiling(CDec(p_stringValue)) Then Return True 
    Return False 
End Function 

Dim _myStringValue As String = "200" 
    If _myStringValue.IsInteger Then 
     'Is an integer 
    Else 
     'Not an integer 
    End If 
相關問題