Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i add a currency symbol before a number?

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

like image 451
sniff_bits Avatar asked Dec 11 '22 08:12

sniff_bits


1 Answers

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!

like image 82
porges Avatar answered Jan 16 '23 00:01

porges