How can I cast a long to double in VBA ? I have googled here and there, and saw some ugly things such as
dim i as long
i = 100
dim d as double
d = i * 1.00000000001
I haven't tested this, I guess this is working, but my application is really data sensitive and I'd like to actually have a cast with no effect on data... Does that not exist ?
“Long,” as the name says, it should hold the value of something big. “Long” is a numerical data type in VBA Excel. The long data type in Excel VBA can hold the values from 0 to 2, 147, 483, 647 for positive numbers, and for the negative number it can hold from 0 to -2, 147, 483, 648.
CInt function example This example uses the CInt function to convert a value to an Integer. Dim MyDouble, MyInt MyDouble = 2345.5678 ' MyDouble is a Double. MyInt = CInt(MyDouble) ' MyInt contains 2346.
The difference between the two are how many digits they can hold. As Single holds 4 bytes of data while As Double can hold 8 bytes. If you want a really, really long floating point number (a number with "point something" at the end) then use As Double, otherwise just use As Single.
You don't need to "cast" anything, this will work fine
dim i as long
dim d as double
i = 100
d = i
(and it took me about 30 seconds to start Excel, press ALT-F11, and test it).
VBA has a bunch of functions that start with 'C' that are used to explicitly cast between types. See CLng, CDbl, CDate, to name a few.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With