Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currency format string in asp.net

I am using an infragistics webgrid and need to format a currency string. For this I need a string containing a pattern such as "$ ### ###,00" and I would like this to come out of my current CultureInfo. How can I do this? Do I need to compose it manually from the info in:

CultureInfo.CreateSpecificCulture(myLanguageId).NumberFormat.CurrencyGroupSeparator
CultureInfo.CreateSpecificCulture(myLanguageId).NumberFormat.CurrencyGroupSizes
CultureInfo.CreateSpecificCulture(myLanguageId).NumberFormat.CurrencyDecimalDigits
CultureInfo.CreateSpecificCulture(myLanguageId).NumberFormat.CurrencyDecimalSeparator

etc etc etc

Is the a one-line solution?

like image 717
Mr W Avatar asked Apr 17 '09 07:04

Mr W


2 Answers

decimal moneyvalue = 1921.39m;
string s = String.Format("{0:C}", moneyvalue);

The current culture will be used.

Make sure you have the following in your web.config:

<system.web>
   <globalization culture="auto" uiCulture="auto"/>
</system.web>

or as ck suggests, declare the equivalent, in your page

like image 98
Greg Dean Avatar answered Nov 09 '22 09:11

Greg Dean


thanks for all your answers. It turns out that my requirements were wrong. The infragistics grid does not want a pattern string to know which separators use, it uses the pattern string for decimals and such and then queries the current culture about the rest. So it is a non-issue. thanks anyway!

like image 34
Mr W Avatar answered Nov 09 '22 09:11

Mr W