Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Format Currency Using .NET Code Vs. Legacy VB6 Code

3.91/5 (3 votes)
31 Mar 2009CPOL2 min read 29K  
To format a string into currency without the dependency of VB6
list-a.gif

Introduction 

This article discusses how 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 further remove VB.NET programmers from the legacy of VB6 and help them to better understand the .NET implementations.

Using the Code 

A.  Visual Basic (VB6) Legacy Function Usage

VB.NET
// 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

VB.NET
// Private Function DoConvert(ByVal Str as String, ByVal DecPlc as Integer) as String
//      Return String.Format("{0:c" & DecPlc & "}",CDec(Str))
// End Function  

The Benchmarks

VB6 Function (Legacy)

Test @ 10,000,000 passes using "5" as the string with 2 decimal places.

  1. Test # 1:  11 seconds 62 milliseconds
  2. Test # 2:  11 seconds 28 milliseconds
  3. Test # 3:  11 seconds 108 milliseconds
Test @ 10,000,000 passes using "500.005" as the string with 2 decimal places.
  1. Test # 1:  11 seconds 673 milliseconds
  2. Test # 2:  11 seconds 113 milliseconds
  3. Test # 3:  11 seconds 397 milliseconds

VB.NET Function

Test @ 10,000,000 passes using "5" as the string with 2 decimal places.

  1. Test # 1:  11 seconds 68 milliseconds
  2. Test # 2:  11 seconds 37 milliseconds
  3. Test # 3:  11 seconds 30 milliseconds 

Test @ 10,000,000 passes using "5" as the string with 2 decimal places.

  1. Test # 1:  13 seconds 456 milliseconds
  2. Test # 2:  12 seconds 449 milliseconds
  3. Test # 3:  12 seconds 827 milliseconds

Known Issues 

Both the VB6 function and the .NET function 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. 

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

History

  • 31st March, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)