Reputation: 409
ToString("C") adds $ symbol before a string. But how do I add other currency types such as Yen, Russian Ruble, Indian Rupee, Swiss Frank?
thanks
Upvotes: 4
Views: 4899
Reputation: 379
To do this you would have to specify the locale of the currency you wish to display. Below is an example of what I believe you are looking for.
object.ToString("C", CultureInfo.CreateSpecificCulture("ja-JP"))
That sample should return the value of object with a Japanese Yen symbol before it. For using different currencies you should refer to:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.createspecificculture(v=vs.110).aspx it contains a list of available options.
For more info on formatting strings look here:
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#CFormatString
Upvotes: 3
Reputation: 30580
You can clone the current culture and modify the currency symbol to whatever you want:
var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.NumberFormat.CurrencySymbol = "!"; // or "¥", etc.
var amount = 1000D;
Console.WriteLine(amount.ToString("C", culture));
This outputs "!1,000.00".
Simply specifying a different culture as the other answers suggest will format the number unexpectedly for the user. For example:
var amount = 1000D;
Console.WriteLine(amount.ToString("C", CultureInfo.CreateSpecificCulture("ru")));
Console.WriteLine(amount.ToString("C", CultureInfo.CreateSpecificCulture("id")));
This outputs:
1 000,00 р.
Rp1.000
Note that the thousands and decimal separators are very different to what I am expecting using the "en-US" culture!
Upvotes: 5
Reputation: 2372
ToString
has an overload that accepts an IFormatProvider
:
Console.WriteLine(amount.ToString("C", CultureInfo.CreateSpecificCulture("ja-JP")));
Sample output: ¥5
More here: http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#CFormatString
Upvotes: 3
Reputation: 89285
Change culture to specific country culture for example :
var no = 1000;
var culture = CultureInfo.CreateSpecificCulture("id-ID");
Console.WriteLine(no.ToString("C", culture));
will print in Indonesian's Rupiah symbol :
Rp1.0000
Upvotes: 3