How do I determine if a variant created from a string is a whole number?

excel, ms-access, vba

Solution

You should write something like:

if cDbl(v) <> round(cDbl(v)) Then

Where cDbl is a function converting any data to a double-type number. You might have to treat cases where v cannot be converted to a number with the isNumeric() function before calling the cDbl function. You can even use the cInt function for your comparisons:

if isnumeric(v) then
    if cDbl(v) - cInt(v) <> 0 Then
    ....
    endif
else
   debug.print "data cannot be converted to a number"
endif

Problem

I am looking to determine if a variant created from a string is a whole number. Here's a test script: ``` dim v as variant v = "42" if v <> round(v) then msgBox("<>") end if ``` The msgBox pops up, probably because the variant was created from a string, although I would have expected v to be = round(v).

Original source

Related problems