System.Globalization.NumberFormatInfo Class

Provides culture-specific information for formatting and parsing numeric values.

See Also: NumberFormatInfo Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public sealed class NumberFormatInfo : ICloneable, IFormatProvider

Remarks

The System.Globalization.NumberFormatInfo class contains culture-specific information that is used when you format and parse numeric values. This information includes the currency symbol, the decimal symbol, the group separator symbol, and the symbols for positive and negative signs.

Instantiating a NumberFormatInfo object

You can instantiate a System.Globalization.NumberFormatInfo object that represents the formatting conventions of the current culture, the invariant culture, a specific culture, or a neutral culture.

Instantiating a NumberFormatInfo object for the current culture

You can instantiate a System.Globalization.NumberFormatInfo object for the current thread culture in any of the following ways. In each case, the returned System.Globalization.NumberFormatInfo object is read-only.

The following example uses these three ways to create System.Globalization.NumberFormatInfo objects that represent the formatting conventions of the current culture. It also retrieves the value of the NumberFormatInfo.IsReadOnly property to illustrate that each object is read-only.

code reference: System.Globalization.NumberFormatInfo.Class.Instantiate#1

You can create a writable System.Globalization.NumberFormatInfo object that represents the conventions of the current thread culture in any of the following ways:

The following example illustrates these two ways of instantiating a System.Globalization.NumberFormatInfo object, and displays the value of its NumberFormatInfo.IsReadOnly property to illustrate that the object is not read-only.

code reference: System.Globalization.NumberFormatInfo.Class.Instantiate#2

Note that the Windows operating system allows the user to override some of the System.Globalization.NumberFormatInfo property values used in numeric formatting and parsing operations through the Region and Language item in Control Panel. For example, a user whose culture is English (United States) might choose to display currency values as 1.1 USD instead of the default of $1.1. The System.Globalization.NumberFormatInfo objects retrieved in the ways discussed previously all reflect these user overrides. If this is undesirable, you can create a System.Globalization.NumberFormatInfo object that does not reflect user overrides (and that is also read/write rather than read-only) by calling the CultureInfo.#ctor(string, bool) constructor and supplying a value of false for the useUserOverride argument. The following example provides an illustration for a system whose current culture is English (United States) and whose currency symbol has been changed from the default of $ to USD.

code reference: System.Globalization.NumberFormatInfo.Class.Instantiate#3

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 System.Globalization.CultureInfo object (for example, if the selected calendar is not one of the calendars listed by the CultureInfo.OptionalCalendars property), the results of the methods and the values of the properties are undefined.

Instantiating a NumberFormatInfo object for the invariant culture

The invariant culture represents a culture that is culture-insensitive. It is based on the English language but not on any specific English-speaking country/region. Although the data of specific cultures can be dynamic and can change to reflect new cultural conventions or user preferences, the data of the invariant culture does not change. A System.Globalization.NumberFormatInfo object that represents the formatting conventions of the invariant culture can be used for formatting operations in which result strings should not vary by culture.

You can instantiate a System.Globalization.NumberFormatInfo object that represents the formatting conventions of the invariant culture in the following ways:

The following example uses each of these methods to instantiate a System.Globalization.NumberFormatInfo object that represents the invariant culture. It then indicates whether the object is read-only,

code reference: System.Globalization.NumberFormatInfo.Class.Instantiate#4

Instantiating a NumberFormatInfo object for a specific culture

A specific culture represents a language that is spoken in a particular country/region. For example, en-US is a specific culture that represents the English language spoken in the United States, and en-CA is a specific culture that represents the English language spoken in Canada. You can instantiate a System.Globalization.NumberFormatInfo object that represents the formatting conventions of a specific culture in the following ways:

The following example uses these four ways to create a System.Globalization.NumberFormatInfo object that reflects the formatting conventions of the Indonesian (Indonesia) culture. It also indicates whether each object is read-only.

code reference: System.Globalization.NumberFormatInfo.Class.Instantiate#5

Instantiating a NumberFormatInfo object for a neutral culture

A neutral culture represents a culture or language that is independent of a country/region. It is typically the parent of one or more specific cultures. For example, fr is a neutral culture for the French language and the parent of the fr-FR culture. You create a System.Globalization.NumberFormatInfo object that represents the formatting conventions of a neutral culture in the same way that you create a System.Globalization.NumberFormatInfo object that represents the formatting conventions of a specific culture.

Note:

In the net_v35_short and earlier versions, trying to retrieve a System.Globalization.NumberFormatInfo object that reflects the formatting conventions of a neutral culture throws a NotSupportedException exception.

However, because it is independent of a specific country/region, a neutral culture lacks culture-specific formatting information. Rather than populating the System.Globalization.NumberFormatInfo object with generic values, the .NET Framework returns a System.Globalization.NumberFormatInfo object that reflects the formatting conventions of a specific culture that is a child of the neutral culture. For example, the System.Globalization.NumberFormatInfo object for the neutral en culture reflects the formatting conventions of the en-US culture, and the System.Globalization.NumberFormatInfo object for the fr culture reflects the formatting conventions of the fr-FR culture.

You can use code like the following to determine which specific culture's formatting conventions each neutral culture represents.

code reference: System.Globalization.NumberFormatInfo.Class.Instantiate#6

NumberFormatInfo and dynamic data

The culture-specific data for formatting numeric values provided by the System.Globalization.NumberFormatInfo class is dynamic, just like the cultural data provided by the System.Globalization.CultureInfo class. You should not make any assumptions about the stability of values for System.Globalization.NumberFormatInfo objects that are associated with particular System.Globalization.CultureInfo objects. Only the data provided by the invariant culture and its associated System.Globalization.NumberFormatInfo object is stable. Other data can change between application sessions, or even within a single session, for the following reasons:

Starting with the .NET Framework 2.0, all user-overridable properties of a System.Globalization.NumberFormatInfo object are initialized when the object is created. There is still a possibility of inconsistency, because neither object creation nor the user override process is atomic, and the relevant values may change during object creation. However, these inconsistencies should be extremely rare.

You can control whether user overrides are reflected in System.Globalization.NumberFormatInfo objects that represent the same culture as the current thread culture. The following table lists the ways in which a System.Globalization.NumberFormatInfo object can be retrieved and indicates whether the resulting object reflects user overrides.

CultureInfo.CurrentCulture.NumberFormat property

Yes

NumberFormatInfo.CurrentInfo property

Yes

CultureInfo.CreateSpecificCulture(string) method

Yes

CultureInfo.GetCultureInfo(string) method

No

CultureInfo.#ctor(string) constructor

Yes

CultureInfo.#ctor(string, bool) constructor

Depends on value of useUserOverride parameter

Unless there is a compelling reason to do otherwise, you should respect user overrides when you use the System.Globalization.NumberFormatInfo object in client applications to format and parse user input or to display numeric data. For server applications or unattended applications, you should not respect user overrides. However, if you are using the System.Globalization.NumberFormatInfo object either explicitly or implicitly to persist numeric data in string form, you should either use a System.Globalization.NumberFormatInfo object that reflects the formatting conventions of the invariant culture, or you should specify a custom numeric format string that you use regardless of culture.

IFormatProvider, NumberFormatInfo, and numeric formatting

A System.Globalization.NumberFormatInfo object is used implicitly or explicitly in all numeric formatting operations. These include calls to the following methods:

All numeric formatting operations make use of an IFormatProvider implementation. The IFormatProvider interface includes a single method, IFormatProvider.GetFormat(Type). This is a callback method that is passed a Type object that represents the type needed to provide formatting information. The method is responsible for returning either an instance of that type or null, if it cannot provide an instance of the type. The .NET Framework provides two IFormatProvider implementations for formatting numbers:

If an IFormatProvider implementation is not provided to a formatting method explicitly, a System.Globalization.CultureInfo object returned by the CultureInfo.CurrentCulture property that represents the current thread culture is used.

The following example illustrates the relationship between the IFormatProvider interface and the System.Globalization.NumberFormatInfo class in formatting operations by defining a custom IFormatProvider implementation. Its IFormatProvider.GetFormat(Type) method displays the type name of the object requested by the formatting operation. If the interface is requesting a System.Globalization.NumberFormatInfo object, this method provides the System.Globalization.NumberFormatInfo object for the current thread culture. As the output from the example shows, the decimal.ToString(IFormatProvider) method requests a System.Globalization.NumberFormatInfo object to provide formatting information, whereas the string.Format(IFormatProvider, string, Object[]) method requests System.Globalization.NumberFormatInfo and System.Globalization.DateTimeFormatInfo objects as well as an ICustomFormatter implementation.

code reference: System.Globalization.NumberFormatInfo.Class#1

If an IFormatProvider implementation is not explicitly provided in a numeric formatting method call, the method calls the CultureInfo.CurrentCulture.GetFormat method, which returns the System.Globalization.NumberFormatInfo object that corresponds to the current thread culture.

Format strings and NumberFormatInfo properties

Every formatting operation uses either a standard or a custom numeric format string to produce a result string from a number. In some cases, the use of a format string to produce a result string is explicit, as in the following example. This code calls the decimal.ToString(IFormatProvider) method to convert a decimal value to a number of different string representations by using the formatting conventions of the en-US culture.

code reference: System.Globalization.NumberFormatInfo.Class#2

In other cases, the use of a format string is implicit. For example, in the following method calls to the default or parameterless decimal.ToString method, the value of the decimal instance is formatted by using the general ("G") format specifier and the conventions of the current culture, which in this case is the en-US culture.

code reference: System.Globalization.NumberFormatInfo.Class#3

Each standard numeric format string uses one or more System.Globalization.NumberFormatInfo properties to determine the pattern or the symbols used in the result string. Similarly, each custom numeric format specifier except "0" and "#" insert symbols in the result string that are defined by System.Globalization.NumberFormatInfo properties. The following table lists the standard and custom numeric format specifiers and their associated System.Globalization.NumberFormatInfo properties. To change the appearance of the result string for a particular culture, see the Modifying NumberFormatInfo properties section. For details about the use of these format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.

"C" or "c" (currency format specifier)

NumberFormatInfo.CurrencyDecimalDigits, to define the default number of fractional digits.

NumberFormatInfo.CurrencyDecimalSeparator, to define the decimal separator symbol.

NumberFormatInfo.CurrencyGroupSeparator, to define the group or thousands separator.

NumberFormatInfo.CurrencyGroupSizes, to define the sizes of integral groups.

NumberFormatInfo.CurrencyNegativePattern, to define the pattern of negative currency values.

NumberFormatInfo.CurrencyPositivePattern, to define the pattern of positive currency values.

NumberFormatInfo.CurrencySymbol, to define the currency symbol.

NumberFormatInfo.NegativeSign, to define the negative sign symbol.

"D" or "d" (decimal format specifier)

NumberFormatInfo.NegativeSign, to define the negative sign symbol.

"E" or "e" (exponential or scientific format specifier)

NumberFormatInfo.NegativeSign, to define the negative sign symbol in the mantissa and exponent.

NumberFormatInfo.NumberDecimalSeparator, to define the decimal separator symbol.

NumberFormatInfo.PositiveSign, to define the positive sign symbol in the exponent.

"F" or "f" (fixed-point format specifier)

NumberFormatInfo.NegativeSign, to define the negative sign symbol.

NumberFormatInfo.NumberDecimalDigits, to define the default number of fractional digits.

NumberFormatInfo.NumberDecimalSeparator, to define the decimal separator symbol.

"G" or "g" (general format specifier)

NumberFormatInfo.NegativeSign, to define the negative sign symbol.

NumberFormatInfo.NumberDecimalSeparator, to define the decimal separator symbol.

NumberFormatInfo.PositiveSign, to define the positive sign symbol for result strings in exponential format.

"N" or "n" (number format specifier)

NumberFormatInfo.NegativeSign, to define the negative sign symbol.

NumberFormatInfo.NumberDecimalDigits, to define the default number of fractional digits.

NumberFormatInfo.NumberDecimalSeparator, to define the decimal separator symbol.

NumberFormatInfo.NumberGroupSeparator, to define the group separator (thousands) symbol.

NumberFormatInfo.NumberGroupSizes, to define the number of integral digits in a group.

NumberFormatInfo.NumberNegativePattern, to define the format of negative values.

"P" or "p" (percent format specifier)

NumberFormatInfo.NegativeSign, to define the negative sign symbol.

NumberFormatInfo.PercentDecimalDigits, to define the default number of fractional digits.

NumberFormatInfo.PercentDecimalSeparator, to define the decimal separator symbol.

NumberFormatInfo.PercentGroupSeparator, to define the group separator symbol.

NumberFormatInfo.PercentGroupSizes, to define the number of integral digits in a group.

NumberFormatInfo.PercentNegativePattern, to define the placement of the percent symbol and the negative symbol for negative values.

NumberFormatInfo.PercentPositivePattern, to define the placement of the percent symbol for positive values.

NumberFormatInfo.PercentSymbol, to define the percent symbol.

"R" or "r" (round-trip format specifier)

NumberFormatInfo.NegativeSign, to define the negative sign symbol.

NumberFormatInfo.NumberDecimalSeparator, to define the decimal separator symbol.

NumberFormatInfo.PositiveSign, to define the positive sign symbol in an exponent.

"X" or "x" (hexadecimal format specifier)

None.

"." (decimal point custom format specifier)

NumberFormatInfo.NumberDecimalSeparator, to define the decimal separator symbol.

"," (group separator custom format specifier)

NumberFormatInfo.NumberGroupSeparator, to define the group (thousands) separator symbol.

"%" (percentage placeholder custom format specifier)

NumberFormatInfo.PercentSymbol, to define the percent symbol.

"‰" (per mille placeholder custom format specifier)

NumberFormatInfo.PerMilleSymbol, to define the per mille symbol.

"E" (exponential notation custom format specifier)

NumberFormatInfo.NegativeSign, to define the negative sign symbol in the mantissa and exponent.

NumberFormatInfo.PositiveSign, to define the positive sign symbol in the exponent.

Note that the System.Globalization.NumberFormatInfo class includes a NumberFormatInfo.NativeDigits property that specifies the base 10 digits used by a specific culture. However, the property is not used in formatting operations; only the Basic Latin digits 0 (U+0030) through 9 (U+0039) are used in the result string. In addition, for float and double values of NaN, PositiveInfinity, and NegativeInfinity, the result string consists exclusively of the symbols defined by the NumberFormatInfo.NaNSymbol, NumberFormatInfo.PositiveInfinitySymbol, and NumberFormatInfo.NegativeInfinitySymbol properties, respectively.

Modifying NumberFormatInfo properties

You can modify the properties of a System.Globalization.NumberFormatInfo object to customize the result string produced in a numeric formatting operation. To do this:

[The 'ordered' type of list has not been implemented in the ECMA stylesheet.]
Note:

Instead of dynamically modifying a culture's property values each time an application is started, you can use the System.Globalization.CultureAndRegionInfoBuilder class to define either a custom culture (a culture that has a unique name and that supplements existing cultures) or a replacement culture (one that is used instead of a specific culture).

The following sections provide some examples.

Modifying the currency symbol and pattern

The following example modifies a System.Globalization.NumberFormatInfo object that that represents the formatting conventions of the en-US culture. It assigns the ISO-4217 currency symbol to the NumberFormatInfo.CurrencySymbol property and defines a pattern for currency values that consists of the currency symbol followed by a space and a numeric value.

code reference: System.Globalization.NumberFormatInfo.Customize#1

Formatting a national identification number

Many national identification numbers consist exclusively of digits and so can easily be formatted by modifying the properties of a System.Globalization.NumberFormatInfo object. For example, a social security number in the United States consists of 9 digits arranged as follows: XXX-XX-XXXX. The following example assumes that social security numbers are stored as integer values and formats them appropriately.

code reference: System.Globalization.NumberFormatInfo.Customize#2

Parsing numeric strings

Parsing involves converting the string representation of a number to a number. Each numeric type in the .NET Framework includes two overloaded parsing methods: Parse and TryParse. The Parse method converts a string to a number and throws an exception if the conversion fails. The TryParse method converts a string to a number, assigns the number to an out argument, and returns a bool value that indicates whether the conversion succeeded.

The parsing methods implicitly or explicitly use a System.Globalization.NumberStyles enumeration value to determine what style elements (such as group separators, a decimal separator, or a currency symbol) can be present in a string if the parsing operation is to succeed. If a System.Globalization.NumberStyles value is not provided in the method call, the default is a System.Globalization.NumberStyles value that includes the NumberStyles.Float and NumberStyles.AllowThousands flags, which specifies that the parsed string can include group symbols, a decimal separator, a negative sign, and white-space characters, or it can be the string representation of a number in exponential notation.

The parsing methods also implicitly or explicitly use a System.Globalization.NumberFormatInfo object that defines the specific symbols and patterns that can occur in the string to be parsed. If a System.Globalization.NumberFormatInfo object is not provided, the default is the System.Globalization.NumberFormatInfo for the current thread culture. For more information about parsing, see the individual parsing methods, such as short.Parse(string), int.Parse(string, NumberStyles), long.Parse(string, IFormatProvider), decimal.Parse(string, NumberStyles, IFormatProvider), double.TryParse(string, Double@), and System.Numerics.BigInteger.TryParse(string, NumberStyles, IFormatProvider, System.Numerics.BigInteger@).

The following example illustrates the culture-sensitive nature of parsing strings. It tries to parse a string that include thousands separators by using the conventions of the en-US, fr-FR, and invariant cultures. A string that includes the comma as a group separator and the period as a decimal separator fails to parse in the fr-FR culture, and a string with white space as a group separator and a comma as a decimal separator fails to parse in the en-US and invariant cultures.

code reference: System.Globalization.NumberFormatInfo.Class#4

Parsing generally occurs in two contexts:

The following sections discuss these two operations in greater detail.

Parsing user strings

When you are parsing numeric strings input by the user, you should always instantiate a System.Globalization.NumberFormatInfo object that reflects the user's cultural settings. For information about how to instantiate a System.Globalization.NumberFormatInfo object that reflects user customizations, see the NumberFormatInfo and dynamic data section.

The following example illustrates the difference between a parsing operation that reflects user cultural settings and one that does not. In this case, the default system culture is en-US, but the user has defined "," as the decimal symbol and "." as the group separator in Control Panel, Region and Language. Ordinarily, these symbols are reversed in the default en-US culture. When the user enters a string that reflects user settings, and the string is parsed by a System.Globalization.NumberFormatInfo object that also reflects user settings (overrides), the parsing operation returns a correct result. However, when the string is parsed by a System.Globalization.NumberFormatInfo object that reflects standard en-US cultural settings, it mistakes the comma symbol for a group separator and returns an incorrect result.

code reference: System.Globalization.NumberFormatInfo.Class#5

Serializing and deserializing numeric data

When numeric data is serialized in string format and later deserialized and parsed, the strings should be generated and parsed by using the conventions of the invariant culture. The formatting and parsing operations should never reflect the conventions of a specific culture. If culture-specific settings are used, the portability of the data is strictly limited; it can be successfully deserialized only on a thread whose culture-specific settings are identical to those of the thread on which it was serialized. In some cases, this means that the data cannot even be successfully deserialized on the same system on which it was serialized.

The following example illustrates what can happen when this principle is violated. Floating-point values in an array are converted to strings when the current thread uses the culture-specific settings of the en-US culture. The data is then parsed by a thread that uses the culture-specific settings of the en-GB culture. In this case, although each parsing operation succeeds, the data does not round-trip successfully and data corruption occurs. In other cases, a parsing operation could fail and a FormatException exception could be thrown.

code reference: System.Globalization.NumberFormatInfo.Class#6

Thread Safety

This type is safe for multithreaded operations.

Requirements

Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0