11.8 Formatting Currency for a Specific Culture

 <  Day Day Up  >  

You want to format a number so that it is displayed as a currency for a given culture.


Technique

Displaying currency information is similar to displaying culturally aware numerical data. Because currency itself is a number, you can use the same technique of passing a NumberFormatInfo object into a method that utilizes an IFormatProvider parameter. To change a number so it is formatted as currency, use the currency format specifier within the method:

 
 private void numericUpDown1_ValueChanged(object sender, System.EventArgs e) {     // demonstrates formatting a value using correct culture number format     NumberFormatInfo currentNFI =         new CultureInfo(cbLanguage.SelectedItem.ToString(),false).NumberFormat;     // C is the currency format specifier     lblCurrency.Text = numericUpDown1.Value.ToString( "C", currentNFI ); } 

Comments

Currency itself is simply a number, and which is why there is no need for a CurrencyFormatInfo object. The NumberFormatInfo class contains several currency- related properties that it uses within its GetFormat method to correctly format a number as a currency value. Also, you only need to specify a NumberFormatInfo object if you want to display a currency value in a culture different from that of the user 's current culture. One thing you should know is that the formatting does not take current exchange rates into consideration.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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