Click here to Skip to main content
Click here to Skip to main content

Format Currency Using .NET Code Vs. Legacy VB6 Code

, 31 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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

// 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("{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)

Share

About the Author

James Mouchett
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
Generalobservation PinmemberShane Story1-Apr-09 12:14 
GeneralVisual Basic namespace PinmemberJeffrey Walton31-Mar-09 8:26 
AnswerRe: Visual Basic namespace PinmemberJames Mouchett1-Apr-09 6:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 31 Mar 2009
Article Copyright 2009 by James Mouchett
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid