VBA Number Types: Currency


General information about "Currency"

From -922337203685477.5808 to 922337203685477.5807

In VBA, a number of the type Currency is a number that can contain values in {-922337203685477.5808,...,-0.0001,0,0.0001,..., 922337203685477.5807}.

The following VBA code works.

When you assign a value outside of the range {-922337203685477.5808,...,-0.0001,0,0.0001,..., 922337203685477.5807}, the VBA code will crash. At run time, it will generate an overflow error.

Hexadecimal assignment

You can use hexadecimal assignment in VBA.

Fractional values

A number of the type Currency can fractional values, however, it only has 4 decimal places. The type Currency is not a floating point number type.

Default

On declaration, by default, VBA assigns the value 0 to the number of the type Currency.

Type declaration character

The type declaration character for Long is @.

Eight bytes

A number of the type Currency takes up eight bytes in the memory. In a byte, there are 8 bits, each of which can take the value 0 or 1. Hence, eight bytes can have 264 possible bit representations. 9223372036854775808 of them are negative, 9223372036854775807 of them are positive, 1 of them is zero. Each of these 18446744073709551616 possible bit representations corresponds to one value in {-922337203685477.5808,...,-0.0001,0,0.0001,..., 922337203685477.5807}.

To illustrate, one possible bit representation could be as follows. The VBA bit representation might be slightly different, but the logic is the same.

BitsValue
00000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000010.0001
00000000000000000000000000000000000000000000000000000000000000100.0002
...
0111111111111111111111111111111111111111111111111111111111111111922337203685477,5807
1000000000000000000000000000000000000000000000000000000000000001-0.0001
10000000000000000000000000000000000000000000000000000000000000100.0002
...
1111111111111111111111111111111111111111111111111111111111111111-922337203685477,5808

Here is how to read the bit representation. For instance, the bit representation 01000001 00000000 0000001 00000000 00000000 00000000 00001001 00000001

Bit 10 x 128=0
Bit 21 x 64=64
Bit 30 x 32=0
Bit 40 x 16=0
Bit 50 x 8=0
Bit 60 x 4=0
Bit 70 x 2=0
Bit 81 x 1=1

corresponds to 65 (= 64 + 1).

Doing +,-,/ etc.

Of course, you can make additions, subtractions, multiplications and so on. For CurrencyNumercial operations are exact, provided that you stay within the range {-922337203685477.5808,...,-0.0001,0,0.0001,..., 922337203685477.5807}.

This VBA code will crash because the result is outside the range {-922337203685477.5808,...,-0.0001,0,0.0001,..., 922337203685477.5807}.

Or, more subtle, and more dangerous.

Of course, division by zero is forbidden. This VBA code will also crash.

Be very careful when doing numerical operations. This VBA code does not crash.

The range {-2147483648,...,-1,0,1,...,2147483647} is quite wide, but not endless. Watch out for overflows. Imagine you have a counter that is augmented with 1 each time you read a line from a file. Probably, when building your code, you will read small files. However, in todays data world, files that have more than 2 billion lines exist and may cause your code to crash. It is not that likely, but be aware.

Conversion to Currency

VBA offers a conversion function CCur() that will try to convert anything into a number of the type Currency.

Bit operations

VBA supports a limited number of bit operations. Better not to use them.