PDA

View Full Version : proper variable type



fb7894
03-04-2018, 08:20 AM
What is the proper variable type to use when dealing with large numbers that contain decimals?

Example: 12,345,678.9012

SamT
03-04-2018, 08:36 AM
Double
There are others, but Doubles are the largest.

Unless I was writing a very memory intensive program, I will always use just Doubles and Longs.

Paul_Hossler
03-04-2018, 06:08 PM
I believe that both Currency and Decimal variables are sub-types of variants and you need to use CDec() and CCur() to convert data


Good write up here with examples

https://msdn.microsoft.com/en-us/VBA/Language-Reference-VBA/articles/type-conversion-functions



Extracts:


https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/currency-data-type




Currency variables (https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/vbe-glossary) are stored as 64-bit (8-byte) numbers in an integer format, scaled by 10,000 to give a fixed-point number with 15 digits to the left of the decimal point and 4 digits to the right. This representation provides a range of -922,337,203,685,477.5808 to 922,337,203,685,477.5807. The type-declaration character (https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/vbe-glossary) for Currency is the at sign ( @ ).

The Currency data type (https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/vbe-glossary) is useful for calculations involving money and for fixed-point calculations in which accuracy is particularly important.




https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/decimal-data-type


Holds signed 128-bit (16-byte) values representing 96-bit (12-byte) integer numbers scaled by a variable power of 10. The scaling factor specifies the number of digits to the right of the decimal point; it ranges from 0 through 28. With a scale of 0 (no decimal places), the largest possible value is +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9228162514264337593543950335E+28). With 28 decimal places, the largest value is +/-7.9228162514264337593543950335, and the smallest nonzero value is +/-0.0000000000000000000000000001 (+/-1E-28). +

The Decimal data type provides the greatest number of significant digits for a number. It supports up to 29 significant digits and can represent values in excess of 7.9228 x 10^28. It is particularly suitable for calculations, such as financial, that require a large number of digits but cannot tolerate rounding errors.