14.3. Overriding Currency Formatting

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.

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:

               Solution
  litCost.Text = sampleValue.ToString("C", _
                                      New CultureInfo("en-US"))
Solution
  litCost.Text = sampleValue.ToString("C", 
                                      new CultureInfo("en-US"));

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, ...

Get ASP.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.