Formatting Currency in VB.NET

Objective:
 
To format a string provided to the function in standard US currency format following the standards of $1,000.00 without using the Visual Basic Namespace (i.e. VB6 or Visual Basic.dll).
 
Speed is highly important.  Benchmarking will be provided to show the differences between the VB6 provided conversion function FormatCurrency and the function created without the use of any VB6 functions.
 
This project is designed to futher remove VB.NET programmers from the legacy of VB6 and help them to better understand the .NET implementations.
 
The Code:
 
A. Visual Basic (VB6) Legacy Function Usage
 
Private Function DoConvert(ByVal Str as String, ByVal DecPlc as Integer) as String
 
     Return Microsoft.VisualBasic.FormatCurrency(Str,DecPlc)
 
End Function
 
B.  VB.NET Function
 
Private Function DoConvert(Byval Str as String, ByVal DecPlc as Integer) as String
 
     Return String.Format(String.Format("{0:c" & DecPlc & "}", CDec(Str))
 
End Function
 
The Benchmark Results:
 
VB6 Function (Legacy)
 
Test @ 10,000,000 passes using "5" as the string with 2 decimal places.
 
Test # 1:  11 seconds 62 milliseconds
 
Test # 2:  11 seconds 28 milliseconds
 
Test # 3:  11 seconds 108 milliseconds
 
Test @ 10,000,000 passes using "500.005" as the string with 2 decimal places 
 
Test # 1:  11 seconds 673 milliseconds
 
Test # 2:  11 seconds 113 milliseconds
 
Test # 3:  11 seconds 397 milliseconds 

 
VB.NET Function
 
Test @ 10,000,000 passes using "5" as the string with 2 decimal places.
 
Test # 1:  11 seconds 68 milliseconds
Test # 2:  11 seconds 37 milliseconds
Test # 3:  11 seconds 30 milliseconds
 
Test @ 10,000,000 passes using "5" as the string with 2 decimal places. 
 
Test # 1:  13 seconds 456 milliseconds
 
Test # 2:  12 seconds 449 milliseconds
 
Test # 3:  12 seconds 827 milliseconds
 
Known Issues:
 
Both the VB6 function and the .NET function both have an inherited issue.  If the value being passed cannot be converted into a decimal number the function will fail.  In order to keep this from happening one must either use some form of a "try/catch" statement or validate the information before passing it to the conversion functions.
 
As a side note, the try/catch statement is excessively slow in comparison to other methods used to validate information.  While it is a good way to catch the error / exception and report it to the screen, it still uses a lot more time and resources to do something that can be prepared for in the beginning.  
 
Program Example Images:
 
Example A:  With Correct Data 
 
Correct-Data-in-VB.NET.gif
 
Example B:  With Incorrect Data 
 
Incorrect-Data-in-VB.NET.gif
 
Conclusion:

As .NET continues to evolve more and more each day, it is important to learn the newer features and coding techniques or procedures to reduce memory usage and increase speed.  While looking above to the benchmarks, we see that the .NET implementation is slightly slower at 10,000,000 tries we must understand that our program will not be using as much memory because it is not lugging around the visualbasic.dll.


Similar Articles