web-dev-qa-db-ja.com

VB2010数値が整数かどうかを見分ける方法

整数が整数なのか、小数なのかを見分ける必要があります。したがって、13は整数になり、23.23は小数になります。

以下のようなので;

If 13 is a whole number then
msgbox("It's a whole number, with no decimals!")
else
msgbox("It has a decimal.")
end if
8
user2691270
If x = Int(x) Then 
  'x is an Integer!'
Else
  'x is not an Integer!'
End If
21
SSS

番号の床と天井が同じかどうかを確認できます。等しい場合は整数です。そうでない場合は異なります。

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

ある型があるという事実から判断すると、それが整数であるか別の型であるかを判断する必要があります。数値は文字列に含まれていると思います。その場合、 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

編集:

10進数または別のタイプを使用して番号を含める場合は、同じアイデアを使用できます。

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
4
Mark Hall

初期値は文字列だと思います。

1、最初に文字列値が数値かどうかを確認します。
2、数値の床と天井を比較します。それが同じなら、あなたは整数を持っています。

私は拡張メソッドを使用することを好みます。

''' <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
1
user3541092
 Dim Num As String = "54.54" 
 If Num.Contains( "。")Then MsgBox( "Decimal")
 '何かをする
0
00yoshi
if x Mod 1 = 0
  'x is an Integer!'
Else
  'x is not an Integer!'
End If
0
Yusuf