Adding A Decimal In Visual Basic - Small Business

We handpick every product we feature. We may earn commission from the links on this page.Small Business|Money & Debt|Money & Debt BasicsAdding a Decimal in Visual BasicBy John Papiewski, Contributor
Visual Basic decimal variables have 29 digits of precision.
Visual Basic decimal variables have 29 digits of precision.Getty

Microsoft's Visual Basic programming language has variable types for three different kinds of numbers: integer, floating-point and decimal. Integer variables range from a short 2-byte format to a long 8-byte format. Floating point variables come in single and double precision. VB has a single decimal variable type that uses 16 bytes of memory and handles up to 29 significant digits.

Variable Declaration

As with any variable in Visual Basic, you declare a decimal type before you use it. To declare the variable, you use the "Dim" statement followed by the variable's name and the words "as decimal." For example, if you want to use the variable "Total_Sale" as a decimal calculation variable, you would use the following Visual Basic statement:

Advertisement

Article continues below this ad

Dim Total_Sale As Decimal

You can declare several decimal variables in the same Dim statement:

Dim Item_Cost, Item_Price, Total_Sale As Decimal

Use

Decimal variables are ideal for calculations requiring more than 16 digits of precision and for handling decimal calculations such as money. Visual Basic's floating point variables have a wider absolute range, up to 10^1023, but with precision limited to 16 digits. Also, because Visual Basic represents decimal variables internally as base-10 and not binary numbers, they resolve calculations involving decimal fractions more accurately than with floating point.

Advertisement

Article continues below this ad

More For You

Can You Save Equations in the iPhone Calculator?

Can You Save Equations in the iPhone Calculator?. Saving equations in a calculator can be...Unlocked iPhone 3Gs work with local carrier SIM cards in virtually any country.

Spacing of Output in Java

Spacing of Output in Java. Java programs without a graphical user interface use a...Grids help guide your eyes up and down long columns of raw worksheet data.

Displaying Unicode With Visual Basic 6

Displaying Unicode With Visual Basic 6. Unicode lets you display extended characters in...Uniode accepts additional symbols, including currency symbols.

What Is a Formula Prefix for a Worksheet?

What Is a Formula Prefix for a Worksheet?. A worksheet is a table in an electronic...Capital lease payments reduce the lease's liability and cash.

How to Change Settings for Fractions to Decimals on the TI-89

How to Change Settings for Fractions to Decimals on the TI-89. The TI-89 is a graphing...The TI-89 brings fractions into the digital age.

Capacity

Decimal variables have an internal scaling counter that automatically keeps track of the decimal point. When dealing with integers, decimal variables range from -79E27 to +79E27. The smallest nonzero number a decimal variable holds is +/- 1E-28. Decimal variables hold up to 29 digits; every digit participates in calculations without rounding. For example, if you store the number "49,999,999,999,999,999,999,999" into a decimal variable and add 1, the result is "50,000,000,000,000,000,000,000." If you attempted this calculation with a floating-point variable, adding 1 would not increase the number as floating-point has insufficient precision.

Speed

In Visual Basic, integer arithmetic is fastest, followed by floating point and decimal. A computer's microprocessor has integer and floating point internal registers for calculations but not those for decimal. This means decimal calculations do not have the benefit of high-speed hardware and are performed entirely in software digit by digit. Though decimal variables have the capacity for constants as big as Avogadro's Number, 6.022E23, or as small as the mass of a proton, 1.67E-24 grams, floating-point numbers are better-suited to scientific calculations as they are much faster.

Advertisement

Article continues below this ad

References

  • MSDN Library: Decimal Data Type
John PapiewskiContributor

Chicago native John Papiewski has a physics degree and has been writing since 1991. He has contributed to "Foresight Update," a nanotechnology newsletter from the Foresight Institute. He also contributed to the book, "Nanotechnology: Molecular Speculations on Global Abundance."

Từ khóa » Visual Basic Double To Decimal