C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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
Also: A powerful option for rounding decimal numbers is the Math.Round Function, which rounds based on specified arguments.
Math.RoundVB.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
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
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
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
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