Decimal Type
This page was last reviewed on Sep 26, 2022.
Dot Net Perls
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.
Function notes. We use special functions, such as Add(), with this type. These functions make Decimal more complex, but also improves the quality of its output.
Example. To start, this program uses the Decimal type. We test the Decimal in an If-statement. We then add two Decimals together, and multiply and subtract Decimals.
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
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.
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
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.
Also A powerful option for rounding decimal numbers is the Math.Round Function, which rounds based on specified arguments.
Module Module1 Sub Main() ' Call Ceiling and Floor. Console.WriteLine(Decimal.Ceiling(1.1)) Console.WriteLine(Decimal.Floor(1.1)) End Sub End Module
2 1
Arithmetic. The Decimal type provides arithmetic Functions. These include Add, Multiply, Subtract and Divide. These Functions make it known that we are using Decimals, not regular Integers.
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
2.4 10 1 5
Parse, TryParse. We 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.
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
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.
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
5 5.5 5 5 5 5 5.5 5 5 5
Memory. How much memory does the Decimal type use? We allocate an array of one million Decimal elements. We then measure how much each element required, on average.
Result The program used about 15 megabytes of memory. With division, we find it required 16 bytes per Decimal element.
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
Program used: 15.3 MB Each decimal element: 16.0 bytes
A summary. Decimal is a large and accurate numeric type—it occupies 16 bytes. We use it with special Functions on the Decimal type. It stores large numbers with additional accuracy.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 26, 2022 (edit).
© 2007-2024 Sam Allen.