Wednesday, August 10, 2011

How to format Currency (Pound/Dollar/Euro etc) using C#/VB.NET/Csharp

Formatting Currency in C#/VB.NET/Csharp Writeline function

You can use .NET's Composite Formatting to format the currency as shown below

Double Incentive = 234.45;
Console.WriteLine("The agreed incentive in local currency is {0:C} ", Incentive);


If you want to convert it into dollars/pounds etc you need to change the culture info :

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
Console.WriteLine("The agreed incentive in USD is {0:C} ", Incentive);


Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB", false);
Console.WriteLine("The agreed incentive in GBP is {0:C} ", Incentive);


The above code needs the following directives

  1. using System.Threading;
  2. using System.Globalization;

No comments: