vb.net VB2010 如何判断一个数是否为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18971322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB2010 How to tell if a number is a whole integer
提问by user2691270
I need to be able to tell if an integer is a whole number or if it has decimals. So 13 would be a whole number and 23.23 would be a decimal.
我需要能够判断一个整数是整数还是小数。所以 13 是一个整数,23.23 是一个小数。
So like;
所以喜欢;
If 13 is a whole number then
msgbox("It's a whole number, with no decimals!")
else
msgbox("It has a decimal.")
end if
回答by SSS
If x = Int(x) Then
'x is an Integer!'
Else
'x is not an Integer!'
End If
回答by rcs
You can check whether the floor and ceiling of the number is the same or not. If it equals, then it is a whole integer, else it will be different.
您可以检查号码的地板和天花板是否相同。如果等于,则为整数,否则不同。
If Math.Floor(value) = Math.Ceiling(value) Then
...
Else
...
End If
回答by Mark Hall
Judging by the fact that you have one type that you are needing to determine whether or not it is an Integer or another Type I am assuming that the number is contained in a string. If so you can use the Integer.TryParseMethod to determine if the value is an Integer, it will also output it as an integer if it is successful. If this isn't what you are doing please update your question with more information.
根据您有一种类型需要确定它是整数还是另一种类型的事实来判断,我假设该数字包含在字符串中。如果是这样可以使用Integer.TryParse方法来判断该值是否为Integer,如果成功也会输出为整数。如果这不是您正在做的事情,请用更多信息更新您的问题。
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
Edit:
编辑:
You can use the same idea if you are using a Decimal or another Type to contain your number,n something like this.
如果您使用 Decimal 或其他类型来包含您的数字,则可以使用相同的想法,n 类似的内容。
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
回答by user3541092
I'm assuming that your initial value is a string.
我假设您的初始值是一个字符串。
1, First check if the string value is numeric.
2, Compare the floor and ceiling of the number. If it's the same, you have a whole number.
1、首先检查字符串值是否为数字。
2、比较地板和天花板的数量。如果相同,则您有一个整数。
I prefer using extension methods.
我更喜欢使用扩展方法。
''' <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
Example:
示例:
Dim _myStringValue As String = "200"
If _myStringValue.IsInteger Then
'Is an integer
Else
'Not an integer
End If
回答by Yusuf
if x Mod 1 = 0
'x is an Integer!'
Else
'x is not an Integer!'
End If
回答by 00yoshi
Dim Num As String = "54.54" If Num.Contains(".") Then MsgBox("Decimal") 'Do Something