Partager via


NumberFormatInfo Class

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

Defines how numeric values are formatted and displayed, depending on the culture.

Inheritance Hierarchy

System.Object
  System.Globalization.NumberFormatInfo

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class NumberFormatInfo _
    Implements IFormatProvider
[ComVisibleAttribute(true)]
public sealed class NumberFormatInfo : IFormatProvider

The NumberFormatInfo type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 NumberFormatInfo Initializes a new writable instance of the NumberFormatInfo class that is culture-independent (invariant).

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CurrencyDecimalDigits Gets or sets the number of decimal places to use in currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CurrencyDecimalSeparator Gets or sets the string to use as the decimal separator in currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CurrencyGroupSeparator Gets or sets the string that separates groups of digits to the left of the decimal in currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CurrencyGroupSizes Gets or sets the number of digits in each group to the left of the decimal in currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CurrencyNegativePattern Gets or sets the format pattern for negative currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CurrencyPositivePattern Gets or sets the format pattern for positive currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CurrencySymbol Gets or sets the string to use as the currency symbol.
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 CurrentInfo Gets a read-only NumberFormatInfo object that contains numeric formatting information for the current culture.
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 InvariantInfo Gets the default read-only NumberFormatInfo object that is culture-independent (invariant).
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsReadOnly Gets a value indicating whether the NumberFormatInfo object is read-only.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NaNSymbol Gets or sets the string that represents the IEEE NaN (not a number) value.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NegativeInfinitySymbol Gets or sets the string that represents negative infinity.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NegativeSign Gets or sets the string that indicates that a number is negative.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NumberDecimalDigits Gets or sets the number of decimal places to use in numeric values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NumberDecimalSeparator Gets or sets the string to use as the decimal separator in numeric values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NumberGroupSeparator Gets or sets the string that separates groups of digits to the left of the decimal in numeric values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NumberGroupSizes Gets or sets the number of digits in each group to the left of the decimal in numeric values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 NumberNegativePattern Gets or sets the format pattern for negative numeric values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PercentDecimalDigits Gets or sets the number of decimal places to use in percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PercentDecimalSeparator Gets or sets the string to use as the decimal separator in percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PercentGroupSeparator Gets or sets the string that separates groups of digits to the left of the decimal in percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PercentGroupSizes Gets or sets the number of digits in each group to the left of the decimal in percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PercentNegativePattern Gets or sets the format pattern for negative percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PercentPositivePattern Gets or sets the format pattern for positive percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PercentSymbol Gets or sets the string to use as the percent symbol.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PerMilleSymbol Gets or sets the string to use as the per mille symbol.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PositiveInfinitySymbol Gets or sets the string that represents positive infinity.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 PositiveSign Gets or sets the string that denotes that the associated number is positive.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Clone Creates a shallow copy of the NumberFormatInfo object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetFormat Gets an object of the specified type that provides a numeric formatting service.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetInstance Gets the NumberFormatInfo object associated with the specified IFormatProvider object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ReadOnly Returns a read-only NumberFormatInfo wrapper.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

This class contains information, such as currency, group (or thousands) separators, and other numeric symbols that are used by a particular culture for formatted numeric values.

To create a NumberFormatInfo object for a specific culture, create a CultureInfo object for that culture and retrieve its CultureInfo.NumberFormat property. To retrieve a NumberFormatInfo object for the current culture, use the NumberFormatInfo.CurrentInfo property. To retrieve a NumberFormatInfo object for the invariant culture, use the NumberFormatInfo.InvariantInfo property for a read-only version, or use the default NumberFormatInfo constructor for a writable version. You cannot create a NumberFormatInfo object for a neutral culture.

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 of the current culture reflects these customizations.

Numeric values are formatted using standard or custom patterns stored in the properties of a NumberFormatInfo object. To modify the display of a value, the application must make the NumberFormatInfo object writable so that custom patterns can be saved in its properties.

The following table lists the standard format specifiers and the associated NumberFormatInfo properties that can be set to modify the standard formats.

Format specifier

Description and associated properties

c, C

Currency format. Associated properties are:

CurrencyNegativePattern, CurrencyPositivePattern, CurrencySymbol, CurrencyGroupSizes, CurrencyGroupSeparator, CurrencyDecimalDigits, CurrencyDecimalSeparator.

d, D

Decimal format.

e, E

Scientific (exponential) format.

f, F

Fixed-point format.

g, G

General format.

n, N

Number format. Associated properties are:

NumberNegativePattern, NumberGroupSizes, NumberGroupSeparator, NumberDecimalDigits, NumberDecimalSeparator.

p, P

Percent format. Use of this format converts a number to a string that represents a percent. The string is defined by the PercentPositivePattern property if the number is positive. The string is defined by the PercentNegativePattern property and starts with a minus sign if the number is negative. The converted number is multiplied by 100 for presentation as a percentage. A precision specifier indicating the desired number of decimal places, for example, "{0:p6}", is allowed. If the precision specifier is omitted, the default numeric precision supported by NumberFormatInfo is used.

r, R

Round-trip format. Use of this format ensures that a floating-point numeric value converted to a string has the same value when the string is converted back to a numeric value. This format is supported only for the Single and Double types. When a numeric value is formatted using this format pattern, it is first tested using the general format, with 15 digits of precision for a Double type and 7 digits of precision for a Single type. If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. However, if the value is not successfully parsed back to the same numeric value, the value is formatted using 17 digits of precision for a Double type and 9 digits of precision for a Single type.

A precision specifier can be appended to the round-trip format pattern, but it is ignored. Round trips are given precedence over precision when using this format pattern.

x, X

Hexadecimal format.

For details about the use of these format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.

A NumberFormatInfo object can be created only for the invariant culture or for specific cultures, not for neutral cultures. For more information about the invariant culture, specific cultures, and neutral cultures, see the CultureInfo class.

NumberFormatInfo implements IFormatProvider to supply formatting information to applications.

Examples

The following example shows how to retrieve a NumberFormatInfo object for a corresponding CultureInfo object, and use the retrieved object to query number formatting information for the particular culture.

Imports System.Globalization
Imports System.Text

Public Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Dim sb As New StringBuilder()

      ' Create an array of English-speaking culture names.
      Dim cultureNames() As String = {"en", "en-AU", "en-BZ", "en-CA", "en-029", _ 
                                      "en-IE", "en-JM", "en-NZ", "en-PH", "en-ZA", _ 
                                      "en-TT", "en-GB", "en-US", "en-ZW" }

      ' Loop through all the culture names.
      For Each cultureName As String In cultureNames
         Dim ci As New CultureInfo(cultureName)
         ' Display the culture name and currency symbol.
         Dim nfi As NumberFormatInfo = ci.NumberFormat
         sb.AppendFormat("The currency symbol for '{0}' is '{1}'", _
                         ci.DisplayName, nfi.CurrencySymbol)
         sb.AppendLine()
      Next
      outputBlock.Text &= sb.ToString() & vbCrLf
   End Sub
End Module
' This example produces the following output.
'    The currency symbol for 'English (United States)' is '$'
'    The currency symbol for 'English (Australia)' is '$'
'    The currency symbol for 'English (Belize)' is 'BZ$'
'    The currency symbol for 'English (Canada)' is '$'
'    The currency symbol for 'English (Caribbean)' is '$'
'    The currency symbol for 'English (Ireland)' is '€'
'    The currency symbol for 'English (Jamaica)' is 'J$'
'    The currency symbol for 'English (New Zealand)' is '$'
'    The currency symbol for 'English (RePublic of the Philippines)' is 'Php'
'    The currency symbol for 'English (South Africa)' is 'R'
'    The currency symbol for 'English (Trinidad and Tobago)' is 'TT$'
'    The currency symbol for 'English (United Kingdom)' is '£'
'    The currency symbol for 'English (United States)' is '$'
'    The currency symbol for 'English (Zimbabwe)' is 'Z$'
using System;
using System.Globalization;
using System.Text;

public sealed class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      StringBuilder sb = new StringBuilder();

      // Create an array of English-speaking culture names.
      string[] cultureNames = {"en", "en-AU", "en-BZ", "en-CA", "en-029", 
                               "en-IE", "en-JM", "en-NZ", "en-PH", "en-ZA", 
                               "en-TT", "en-GB", "en-US", "en-ZW" };

      // Loop through all the culture names.
      foreach (string cultureName in cultureNames)
      {
         CultureInfo ci = new CultureInfo(cultureName);

         // Display the culture name and currency symbol.
         NumberFormatInfo nfi = ci.NumberFormat;
         sb.AppendFormat("The currency symbol for '{0}' is '{1}'",
             ci.DisplayName, nfi.CurrencySymbol);
         sb.AppendLine();
      }
      outputBlock.Text += sb.ToString() + "\n";
   }
}
// This example produces the following output.
//    The currency symbol for 'English (United States)' is '$'
//    The currency symbol for 'English (Australia)' is '$'
//    The currency symbol for 'English (Belize)' is 'BZ$'
//    The currency symbol for 'English (Canada)' is '$'
//    The currency symbol for 'English (Caribbean)' is '$'
//    The currency symbol for 'English (Ireland)' is '�'
//    The currency symbol for 'English (Jamaica)' is 'J$'
//    The currency symbol for 'English (New Zealand)' is '$'
//    The currency symbol for 'English (Republic of the Philippines)' is 'Php'
//    The currency symbol for 'English (South Africa)' is 'R'
//    The currency symbol for 'English (Trinidad and Tobago)' is 'TT$'
//    The currency symbol for 'English (United Kingdom)' is '�'
//    The currency symbol for 'English (United States)' is '$'
//    The currency symbol for 'English (Zimbabwe)' is 'Z$'

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.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.