Currency format for display
This kind of functionality is built in.
When using a decimal you can use a format string "C" or "c".
decimal dec = 123.00M;
string uk = dec.ToString("C", new CultureInfo("en-GB")); // uk holds "£123.00"
string us = dec.ToString("C", new CultureInfo("en-US")); // us holds "$123.00"
Try the Currency Format Specifier ("C"). It automatically takes the current UI culture into account and displays currency values accordingly.
You can use it with either String.Format
or the overloaded ToString
method for a numeric type.
For example:
decimal value = 12345.6789M; // Be sure to use Decimal for money values. Do not use IEEE-754 types such as float (System.Single) and double (System.Double) as they can only store approximate values.
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));
Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));
Console.WriteLine(value.ToString("C3", CultureInfo.CreateSpecificCulture("da-DK")));
// The example displays the following output on a system whose
// current culture is English (United States):
// $12,345.68
// $12,345.679
// kr 12.345,679