Export (0) Print
Expand All

NumberFormatInfo Class

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

System.Object
  System.Globalization.NumberFormatInfo

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

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class NumberFormatInfo : ICloneable, 
	IFormatProvider

The NumberFormatInfo type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryNumberFormatInfoInitializes a new writable instance of the NumberFormatInfo class that is culture-independent (invariant).
Top

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCurrencyDecimalDigitsGets or sets the number of decimal places to use in currency values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCurrencyDecimalSeparatorGets or sets the string to use as the decimal separator in currency values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCurrencyGroupSeparatorGets or sets the string that separates groups of digits to the left of the decimal in currency values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCurrencyGroupSizesGets or sets the number of digits in each group to the left of the decimal in currency values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCurrencyNegativePatternGets or sets the format pattern for negative currency values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCurrencyPositivePatternGets or sets the format pattern for positive currency values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCurrencySymbolGets or sets the string to use as the currency symbol.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCurrentInfoGets a read-only NumberFormatInfo that formats values based on the current culture.
Public propertyDigitSubstitutionGets or sets a value that specifies how the graphical user interface displays the shape of a digit.
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryInvariantInfoGets the default read-only NumberFormatInfo that is culture-independent (invariant).
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryIsReadOnlyGets a value indicating whether the NumberFormatInfo is read-only.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNaNSymbolGets or sets the string that represents the IEEE NaN (not a number) value.
Public propertyNativeDigitsGets or sets a string array of native digits equivalent to the Western digits 0 through 9.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNegativeInfinitySymbolGets or sets the string that represents negative infinity.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNegativeSignGets or sets the string that denotes that the associated number is negative.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNumberDecimalDigitsGets or sets the number of decimal places to use in numeric values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNumberDecimalSeparatorGets or sets the string to use as the decimal separator in numeric values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNumberGroupSeparatorGets or sets the string that separates groups of digits to the left of the decimal in numeric values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNumberGroupSizesGets or sets the number of digits in each group to the left of the decimal in numeric values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryNumberNegativePatternGets or sets the format pattern for negative numeric values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPercentDecimalDigitsGets or sets the number of decimal places to use in percent values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPercentDecimalSeparatorGets or sets the string to use as the decimal separator in percent values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPercentGroupSeparatorGets or sets the string that separates groups of digits to the left of the decimal in percent values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPercentGroupSizesGets or sets the number of digits in each group to the left of the decimal in percent values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPercentNegativePatternGets or sets the format pattern for negative percent values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPercentPositivePatternGets or sets the format pattern for positive percent values.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPercentSymbolGets or sets the string to use as the percent symbol.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPerMilleSymbolGets or sets the string to use as the per mille symbol.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPositiveInfinitySymbolGets or sets the string that represents positive infinity.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryPositiveSignGets or sets the string that denotes that the associated number is positive.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryCloneCreates a shallow copy of the NumberFormatInfo.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetFormatGets an object of the specified type that provides a number formatting service.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryGetInstanceGets the NumberFormatInfo associated with the specified IFormatProvider.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryReadOnlyReturns a read-only NumberFormatInfo wrapper.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

This class contains information, such as currency, decimal separators, and other numeric symbols.

To create a NumberFormatInfo object for a specific culture, your application creates a CultureInfo object for that culture and retrieves the CultureInfo.NumberFormat property. To create a NumberFormatInfo object for the culture of the current thread, the application uses the CurrentInfo property. To create a NumberFormatInfo object for the invariant culture, the application uses the InvariantInfo property for a read-only version, or uses the NumberFormatInfo constructor for a writable version. It is not possible to create a NumberFormatInfo object for a neutral culture.

The user might choose to override some of the values associated with the current culture of Windows through the regional and language options in Control Panel. 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. If the CultureInfo.UseUserOverride property is set to true, the properties CultureInfo.DateTimeFormat, CultureInfo.NumberFormat, and CultureInfo.TextInfo are also retrieved from the user settings. If the user settings are incompatible with the culture associated with the CultureInfo object, for example, if the selected calendar is not one of the OptionalCalendars, the results of the methods and the values of the properties are undefined.

For versions of .NET Framework prior to 2.0, if the application sets the CultureInfo.UseUserOverride property to true, the object reads each user-overridable property only when it is accessed for the first time. Because NumberFormatInfo has more than one user-overridable property, this "lazy initialization" can lead to an inconsistency between such properties when the application accesses one property, the user changes to another culture or overrides properties of the current user culture through regional and language options in Control Panel, then the application accesses a different property. For example, in a sequence like this, CurrencyGroupSeparator can be accessed. Then the user can change patterns in the Control Panel, and CurrencyDecimalSeparator, when accessed, follows the new settings. A similar inconsistency occurs when the user changes the user culture in the Control Panel.

In .NET Framework version 2.0 and later, NumberFormatInfo does not use this "lazy initialization." Instead, it reads all user-overridable properties when it is created. There is still a small window of vulnerability in which neither object creation nor the user override process is atomic, and the relevant values can change during object creation. However, the occurrence of this vulnerability is extremely rare.

Numeric values are formatted using standard or custom patterns stored in the properties of a NumberFormatInfo. 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 describes the format patterns for each standard format specifier and the associated NumberFormatInfo properties that can be set to modify the standard formats. For details about the use of these patterns, see Standard Numeric Format Strings.

Format pattern

Description and associated properties

c, C

Currency format.

Associated properties: CurrencyDecimalDigits, CurrencyDecimalSeparator, CurrencyGroupSeparator, CurrencyGroupSizes, CurrencyNegativePattern, CurrencyPositivePattern, CurrencySymbol, NegativeSign

d, D

Decimal format.

Associated properties: NegativeSign

e, E

Scientific (exponential) format.

Associated properties: NegativeSign, NumberDecimalSeparator, PositiveSign

f, F

Fixed-point format.

Associated properties: NegativeSign, NumberDecimalDigits, NumberDecimalSeparator

g, G

General format.

Associated properties: NegativeSign, NumberDecimalDigits, NumberDecimalSeparator, PositiveSign

n, N

Number format.

Associated properties: NegativeSign,NumberDecimalDigits, NumberDecimalSeparator, NumberGroupSeparator, NumberGroupSizes, NumberNegativePattern

p, P

Percent format.

Associated properties: NegativeSign,PercentDecimalDigits, PercentDecimalSeparator, PercentGroupSeparator, PercentGroupSizes, PercentNegativePattern, PercentPositivePattern, PercentSymbol

r, R

Round-trip format.

Associated properties: NegativeSign, NumberDecimalSeparator, PositiveSign

x, X

Hexadecimal format.

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 the ICloneable interface to enable duplication of NumberFormatInfo objects. It also implements IFormatProvider to supply formatting information to applications.

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.


using System;
using System.Globalization;
using System.Text;

public sealed class App 
{
    static void Main() 
    {
        StringBuilder sb = new StringBuilder();

        // Loop through all the specific cultures known to the CLR.
        foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 
        {
            // Only show the currency symbols for cultures that speak English.
            if (ci.TwoLetterISOLanguageName != "en") continue;

            // 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();
        }
        Console.WriteLine(sb.ToString());
    }
}

// This code produces the following output.
//
// The currency symbol for 'English (United States)' is '$'
// The currency symbol for 'English (United Kingdom)' is '�'
// The currency symbol for 'English (Australia)' is '$'
// The currency symbol for 'English (Canada)' is '$'
// The currency symbol for 'English (New Zealand)' is '$'
// The currency symbol for 'English (Ireland)' is '?'
// The currency symbol for 'English (South Africa)' is 'R'
// The currency symbol for 'English (Jamaica)' is 'J$'
// The currency symbol for 'English (Caribbean)' is '$'
// The currency symbol for 'English (Belize)' is 'BZ$'
// The currency symbol for 'English (Trinidad and Tobago)' is 'TT$'
// The currency symbol for 'English (Zimbabwe)' is 'Z$'
// The currency symbol for 'English (Republic of the Philippines)' is 'Php'


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

Community Additions

ADD
Show:
© 2014 Microsoft