Get Integer Value of a Double

I want to get the integer value of a double. So if the number is 1234.56, I want to return 1234. The only method I have found is to first convert the double to a string and then extract the integer value from that, using… CLong(CStr(dNumber)). That’s such a kludgey way to do something like that, so I thought I would ask if there is a more obvious method.

Used to perform Integer division between two numbers.

result=expression1 \ expression2

Dim v as Integer
Dim d as double


Use Floor() to get just the integer portion.

Floor(1234.99) = 1234

The result of a Floor() is still double.
You could multiply it by 1 (integer value) to get the integer

dim d as double = 3.14159265 dim r as integer r = d * 1

result is an integer.

This implicit cast conversion doesn’t need multiplication. :wink:

dim d as double = 3.14159265
dim r as integer = d // … r = 3

1 Like

There is one gotcha with Floor() due its nature, negative numbers. Xojo could have a Trunc(). :slight_smile:

dim d as Double = -3.99
dim f as integer = Floor(d) //___ Returns: -4 <==
dim i as integer = d //___ Returns: -3

You could also put the value into a Variant and get the integer value with the according function :wink:

dim d as double = 5.55
dim i as integer = CType(d,Integer)