Reputation: 545
I do have values and currency symbols in my database (BRL, USD and etc) and would like use that to write a formatted string, something like:
int number = 100;
string currencySymbol = "USD";
string formattedNumber = number.ToString("C", currencySymbol);
I've tried to cast currencySymbol to cultureInfo but it is not possible as written in this post get CultureInfo from a Currency Code?
Upvotes: 0
Views: 1626
Reputation: 223237
I believe you are looking to use custom currency symbols in formatting. You can do:
int number = 100;
var numberFormatInfo = (NumberFormatInfo) NumberFormatInfo.CurrentInfo.Clone();
numberFormatInfo.CurrencySymbol = "USD";
string formattedNumber = number.ToString("C", numberFormatInfo);
A better approach would be to store different culture information ("en-US") in database and then retrieve the culture based on it and use its predefined currency symbol. But it will not be same as yours. In case of US, it is $
and not USD
.
Upvotes: 3