How to check whether a floating-point variable is an integer (has no fractional part) in VB.NET

2 Answers

0 votes
Imports System

Friend Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim d As Double = 24278.0

        If d = Math.Floor(d) Then
            Console.WriteLine("The double variable contains an integer")
        Else
            Console.WriteLine("The double variable contains a floating point")
        End If
    End Sub
End Class




' run:
'
' The double variable contains an integer
'

 



answered Mar 5, 2024 by avibootz
0 votes
Imports System

' -----------------------------------------------------------------------------
' Determines whether a Double contains an integer (no fractional part).
' Uses a small tolerance to safely handle floating‑point precision issues.
' -----------------------------------------------------------------------------
Module Program

    Function IsInteger(value As Double) As Boolean
        Dim epsilon As Double = 1E-12

        ' Math.Floor returns the largest integer <= value
        Dim floored As Double = Math.Floor(value)

        ' If the difference is extremely small, treat the number as an integer
        Return Math.Abs(value - floored) < epsilon
    End Function

    ' -----------------------------------------------------------------------------
    ' Prints a descriptive message for the given Double.
    ' -----------------------------------------------------------------------------
    Sub PrintCheck(value As Double)
        Console.WriteLine($"Checking value: {value:F15}")

        If IsInteger(value) Then
			Console.WriteLine("Result: The value contains an integer." & Environment.NewLine)
        Else
            Console.WriteLine("Result: The value contains a floating‑point number." & Environment.NewLine)
        End If
    End Sub

    ' -----------------------------------------------------------------------------
    ' Entry point demonstrating several example values.
    ' -----------------------------------------------------------------------------
    Sub Main()
        ' Example input values
        Dim a As Double = 42.0                    ' integer-like
        Dim b As Double = 42.00000000001          ' floating-point due to tiny fraction
        Dim c As Double = 89.0000000000001        ' integer-like
        Dim d As Double = -7.0                    ' negative integer-like
        Dim e As Double = 3.14                    ' floating-point

        ' Run checks
        PrintCheck(a)
        PrintCheck(b)
        PrintCheck(c)
        PrintCheck(d)
        PrintCheck(e)
    End Sub

End Module


'
' run:
'
' Checking value: 42.000000000000000
' Result: The value contains an integer.
' 
' Checking value: 42.000000000009997
' Result: The value contains a floating‑point number.
' 
' Checking value: 89.000000000000099
' Result: The value contains an integer.
' 
' Checking value: -7.000000000000000
' Result: The value contains an integer.
' 
' Checking value: 3.140000000000000
' Result: The value contains a floating‑point number.
'

 



answered Sep 10 by avibootz

Related questions

...