VB2010 How to determine if a number is an integer

I need to find out if an integer or if it has decimal numbers. So 13 will be an integer, and 23.23 will be a decimal.

So that,

If 13 is a whole number then msgbox("It a whole number, with no decimals!") else msgbox("It has a decimal.") end if 
+6
source share
5 answers
 If x = Int(x) Then 'x is an Integer!' Else 'x is not an Integer!' End If 
+17
source

You can check whether the floor and ceiling of the room are the same or not. If it is equal, then this is an integer, otherwise it will be different.

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

Judging by the fact that you have one type that you need to determine if it is an integer or another type, I assume that the number is contained in the string. If so, you can use the Integer.TryParse method to determine if this value is an integer, it also displays it as an integer if it succeeds, If that is not what you are doing, update your question with more information.

 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 use decimal or another type to contain your number, n something like this.

 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
source

I assume your initial value is a string.

1 . First check if the string value is numeric.

2 . Compare the floor and ceiling of the room. If this is the same, you have an integer.

I prefer to use 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 

An example :

  Dim _myStringValue As String = "200" If _myStringValue.IsInteger Then 'Is an integer Else 'Not an integer End If 
+1
source
  Dim Num As String = "54.54"
 If Num.Contains (".") Then MsgBox ("Decimal") 
 'Do something
0
source

Source: https://habr.com/ru/post/954466/


All Articles