Recipe 14.3 Overriding Currency Formatting

     

14.3.1 Problem

You have an international application that must always display currency with the U.S. dollar symbol but all other text must follow the language setting in the browser.

14.3.2 Solution

Implement the solution described in Recipe 14.2, and then use a CultureInfo object to override the default currency format of the currency value in the code-behind class for the page, as shown here:

 
figs/vbicon.gif
 litCost.Text = sampleValue.ToString("C", _ New CultureInfo("en-US")) 
figs/csharpicon.gif
 litCost.Text = sampleValue.ToString("C", new CultureInfo("en-US")); 

14.3.3 Discussion

It is not uncommon to have an international application that needs to display dates, a currency value, or other data in a format specific to a language or culture that is not the default. For example, you might need to display currency values in U.S. format but all text and dates in the local language of the user .

The simplest solution in such cases is to implement the example described in Recipe 14.2 and then use the ToString method of the number to override its default currency setting. Use the " C " string format code to indicate the number should be formatted as a currency value and a CultureInfo object to require U.S. English with the en-US language code. If you have many places where the alternate currency formatting is required, you should implement a single method to perform it. This will make changes much simpler when they are required.

If your application does not need to display non-currency values such as dates or other numbers in the user's local language format, an even simpler solution is to leave the CurrentCulture property of the current thread unset. This way all formatting of dates and numbers will follow the regional settings of your web server without any additional coding.



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2006
Pages: 179

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net