VB2010如何判断一个数字是否为整数

10

我需要能够判断一个整数是否为整数或者它是否有小数位。因此,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

你用来存储数字的变量是什么类型?它是字符串吗? - Mark Hall
6个回答

25
If x = Int(x) Then 
  'x is an Integer!'
Else
  'x is not an Integer!'
End If

1
当x是一个字符串时会发生什么?例如:"2","2.2","3.14"?或者更好的是,当x大于21亿时会发生什么?整数(例如表ID)可以超过Int(@ 21亿)中可以存储的最大值。 - user3541092
1
我假设了严格的类型。如果数字太大超出了变量范围,你将会得到一个溢出异常。 - SSS

11

您可以检查一个数字的整数部分和上下取整是否相等。如果相等,则为整数,否则不是。

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

2
或者您可以尝试使用 Math.Truncate(value) = value 来判断。 - Stef Geysels

5
根据您需要确定一个类型是整数还是另一种类型,我猜测这个数字包含在一个字符串中。如果是这样,您可以使用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

抱歉,我已更新原帖。输入始终为整数,但我需要确定它是一个整数还是有小数位的整数。 - user2691270
2
@user2691270,一个整数不能有小数点,如果有小数点,它可能是Decimal、Double、String、Single或类似类型之一。这个数字是从哪里生成的?如果是从一个文本框中获取的,它很可能是一个字符串。 - Mark Hall
2
@MarkHall:如果您对结果不感兴趣,则不需要“helper”变量来尝试解析。您可以传递一个常量(如0、-1、42或任何其他值),或者“nothing”(默认为整数0)。 顺便说一句:由于OP仅指定了“整数”,我建议使用BigInteger.TryParse。 - igrimpe

1
Dim Num As String = "54.54"
如果 Num 包含 "." 则 MsgBox("Decimal")
'做些什么

2
一些详细的阐述和解释会提高这个答案的质量。 - Jon Surrell
这个答案是唯一一个检查数字(作为字符串)格式是否正确的答案。其他答案将1等同于1.00,这是错误的。 - scott_f

1
我假设您的初始值是一个字符串。 1、首先检查字符串值是否为数字。 2、比较数字的floor和ceiling。如果相同,则有一个整数。
我更喜欢使用扩展方法。
''' <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

0
if x Mod 1 = 0
  'x is an Integer!'
Else
  'x is not an Integer!'
End If

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接