VB.NET Decimal Type

Use the Decimal type and call Add and Multiply on Decimal. A Decimal stores 16 bytes.

Decimal. The Decimal type stores large numbers. It requires 16 bytes of memory, so it rarely truncates values. But this makes it less efficient and more awkward to use. We use special methods, such as Add(), with this type.

Example. To start, this program uses the Decimal type. It declares a Dim "d" that is of Decimal type. We test the Decimal in an If-statement. We then add two Decimals together, and multiply and subtract Decimals.
VB.NET program that uses Decimal Module Module1 Sub Main() Dim d As Decimal = 1.0 ' Test Decimal. If d = 1.0 Then Console.WriteLine(d) End If Dim y As Decimal = 0.5 ' Add Decimal. y += d Console.WriteLine(y) ' Subtract and multiply. y -= (d * 2) Console.WriteLine(y) End Sub End Module Output 1 1.5 -0.5

Constants. Several constants are available on the Decimal type. These include the MaxValue, MinValue, One, Zero and MinusOne constants. We can use these constants to get the expected value. MaxValue and MinValue are most useful.
VB.NET program that uses Decimal constants Module Module1 Sub Main() Console.WriteLine(Decimal.MaxValue) Console.WriteLine(Decimal.MinValue) Console.WriteLine(Decimal.One) Console.WriteLine(Decimal.Zero) Console.WriteLine(Decimal.MinusOne) End Sub End Module Output 79228162514264337593543950335 -79228162514264337593543950335 1 0 -1

Floor, Ceiling. The Decimal.Floor Function removes the fractional part of the Decimal value. And the Decimal.Ceiling Function always rounds up if the Decimal value has a fractional value. These Functions are similar to Math.Floor and Ceiling.

Also: A powerful option for rounding decimal numbers is the Math.Round Function, which rounds based on specified arguments.

VB.NET program that uses Decimal.Ceiling, Floor Module Module1 Sub Main() ' Call Ceiling and Floor. Console.WriteLine(Decimal.Ceiling(1.1)) Console.WriteLine(Decimal.Floor(1.1)) End Sub End Module Output 2 1

Arithmetic. The Decimal type provides arithmetic Functions. These include Add, Multiply, Subtract and Divide. With these, we avoid using operators such as "+". These Functions make it known that we are using Decimals, not regular Integers.
VB.NET program that uses Add, Multiply, Subtract, Divide Module Module1 Sub Main() Dim result1 = Decimal.Add(1.1, 1.3) Console.WriteLine(result1) Dim result2 = Decimal.Multiply(5.0, 2.0) Console.WriteLine(result2) Dim result3 = Decimal.Subtract(2.0, 1.0) Console.WriteLine(result3) Dim result4 = Decimal.Divide(10.0, 2.0) Console.WriteLine(result4) End Sub End Module Output 2.4 10 1 5

Parse, TryParse. As with other numeric types in the .NET Framework, you can use the Parse and TryParse shared methods on Decimal. This program first parses the string "100.01". It then tries to parse (with TryParse) an invalid number.

And: No exception is thrown by TryParse. It uses a tester-doer pattern. It instead just returns false on invalid values.

VB.NET program that uses Parse, TryParse Module Module1 Sub Main() ' Use Parse for valid arguments. Dim value As Decimal = Decimal.Parse("100.01") Console.WriteLine(value) ' Use TryParse for arguments that might be invalid. Dim value2 As Decimal If Decimal.TryParse("perl", value2) Then Console.WriteLine("Not reached") End If End Sub End Module Output 100.01

Convert. Many conversion methods are available on the Decimal type. We can convert a Decimal into a Byte, Double, Integer (including Shorts and Longs), or UInteger. These Functions are clearer than using casts, and may be more reliable.Integer
VB.NET program that converts Decimal Module Module1 Sub Main() Dim input As Decimal = 5.5 Console.WriteLine(Decimal.ToByte(input)) Console.WriteLine(Decimal.ToDouble(input)) Console.WriteLine(Decimal.ToInt16(input)) Console.WriteLine(Decimal.ToInt32(input)) Console.WriteLine(Decimal.ToInt64(input)) Console.WriteLine(Decimal.ToSByte(input)) Console.WriteLine(Decimal.ToSingle(input)) Console.WriteLine(Decimal.ToUInt16(input)) Console.WriteLine(Decimal.ToUInt32(input)) Console.WriteLine(Decimal.ToUInt64(input)) End Sub End Module Output 5 5.5 5 5 5 5 5.5 5 5 5

Memory. How much memory does the Decimal type use? In this program, we answer this question. We allocate an array of one million Decimal elements. We then measure how much each element required, on average.Array

Result: The program used about 15 megabytes of memory. With division, we find it required 16 bytes per Decimal element.

VB.NET program that measures Decimal memory Module Module1 Sub Main() ' Measure memory and allocate array of Decimals. Dim bytes1 As Long = GC.GetTotalMemory(True) Dim arr1(1000000) As Decimal Dim bytes2 As Long = GC.GetTotalMemory(True) arr1(0) = 0 ' Compute decimal sizes. Dim difference As Long = bytes2 - bytes1 Dim per As Double = difference / 1000000 Console.WriteLine("Program used: {0:0.0} MB", difference / (1024 * 1024)) Console.WriteLine("Each decimal element: {0:0.0} bytes", per) End Sub End Module Output Program used: 15.3 MB Each decimal element: 16.0 bytes

Summary. Decimal is a large and accurate numeric type. It occupies 16 bytes. We use it with special Functions on the Decimal type. But in many ways, it is similar to other numeric types. It stores large numbers with additional accuracy.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to