Condividi tramite


CultureInfo.NumberFormat Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets a NumberFormatInfo object that defines the culturally appropriate format for converting numbers, currency values, and percentages to strings.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Property NumberFormat As NumberFormatInfo
public virtual NumberFormatInfo NumberFormat { get; set; }

Property Value

Type: System.Globalization.NumberFormatInfo
An object that defines the culturally appropriate format for outputting numbers, currency values, and percentages.

Exceptions

Exception Condition
ArgumentNullException

The property is being set to nulla null reference (Nothing in Visual Basic).

InvalidOperationException

The NumberFormat property or any of the NumberFormatInfo properties is being set, and the CultureInfo is read-only.

Remarks

The user might choose to override some of the values associated with the current system culture. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture. The NumberFormatInfo object returned by the NumberFormat property of the current culture reflects these overrides.

The values of the DateTimeFormat property and the NumberFormat property are not calculated until the property is accessed. If the application changes the current culture to a new culture while the application is running and then accesses the DateTimeFormat or NumberFormat property, the application retrieves the defaults for the new culture rather than the overrides for the original culture. To preserve the overrides for the original current culture, the application should access the DateTimeFormat and NumberFormat properties before changing the current culture.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 If Name is fr, and the CurrencySymbol property of NumberFormat is set to USD, NumberFormat throws a NotSupportedException.

Examples

The following example shows that Clone method also clones the DateTimeFormatInfo and NumberFormatInfo objects associated with the CultureInfo object.

Imports System.Globalization

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Creates and initializes a CultureInfo object.
      Dim myCI As New CultureInfo("en-US")

      ' Clones myCI and modifies the DTFI and NFI instances associated with the clone.
      Dim myCIclone As CultureInfo = CType(myCI.Clone(), CultureInfo)
      myCIclone.DateTimeFormat.AMDesignator = "a.m."
      myCIclone.NumberFormat.CurrencySymbol = "USD"
      myCIclone.NumberFormat.NumberDecimalDigits = 4

      ' Displays the properties of the DTFI and NFI instances associated with the original and with the clone. 
      outputBlock.Text &= "DTFI/NFI PROPERTY" + ControlChars.Tab + "ORIGINAL" + ControlChars.Tab + "MODIFIED CLONE" & vbCrLf
      outputBlock.Text += String.Format("DTFI.AMDesignator" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator) & vbCrLf
      outputBlock.Text += String.Format("NFI.CurrencySymbol" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol) & vbCrLf
      outputBlock.Text += String.Format("NFI.NumberDecimalDigits" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits) & vbCrLf
   End Sub  

End Class 
' This example produces the following output.
'       DTFI/NFI PROPERTY       ORIGINAL        MODIFIED CLONE
'       DTFI.AMDesignator       AM              a.m.
'       NFI.CurrencySymbol      $               USD
'       NFI.NumberDecimalDigits 2               4
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Creates and initializes a CultureInfo object.
      CultureInfo myCI = new CultureInfo("en-US");

      // Clones myCI and modifies the DTFI and NFI instances associated with the clone.
      CultureInfo myCIclone = (CultureInfo)myCI.Clone();
      myCIclone.DateTimeFormat.AMDesignator = "a.m.";
      myCIclone.NumberFormat.CurrencySymbol = "USD";
      myCIclone.NumberFormat.NumberDecimalDigits = 4;

      // Displays the properties of the DTFI and NFI instances associated with the original and with the clone. 
      outputBlock.Text += "DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE" + "\n";
      outputBlock.Text += String.Format("DTFI.AMDesignator\t{0}\t\t{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator) + "\n";
      outputBlock.Text += String.Format("NFI.CurrencySymbol\t{0}\t\t{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol) + "\n";
      outputBlock.Text += String.Format("NFI.NumberDecimalDigits\t{0}\t\t{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits) + "\n";
   }
}
/*
This code produces the following output.
   DTFI/NFI PROPERTY       ORIGINAL        MODIFIED CLONE
   DTFI.AMDesignator       AM              a.m.
   DTFI.DateSeparator      /               -
   NFI.CurrencySymbol      $               USD
   NFI.NumberDecimalDigits 2               4
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.