Parse currency values in C#

Converting a decimal value into a currency formatted string is easy. Simply use its ToString method, passing it the parameter "C" as in the following code.

txtAny.Text = value.ToString("C");

Strangely parsing a currency value entered by the user is not as simple. By default, decimal.Parse doesn't understand currency symbols or parentheses used to indicate negative values.

The solution is to pass decimal.Parse the second parameter NumberStyles.Any, which is defined in the System.Globalization namespace. The following code correctly parses currency values.

decimal value = decimal.Parse(txtValue.Text, NumberStyles.Any);

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.