Export (0) Print
Expand All

NumberFormatInfo Class

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

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class NumberFormatInfo : ICloneable, IFormatProvider
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class NumberFormatInfo implements ICloneable, IFormatProvider
SerializableAttribute 
ComVisibleAttribute(true) 
public final class NumberFormatInfo implements ICloneable, IFormatProvider

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

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

The user might choose to override some of the values associated with the current culture of Windows through Regional and Language Options (or Regional Options or Regional Settings) 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 of the CultureInfo.DateTimeFormat instance, the CultureInfo.NumberFormat instance, and the CultureInfo.TextInfo instance are also retrieved from the user settings. If the user settings are incompatible with the culture associated with the CultureInfo (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.

Numeric values are formatted using standard or custom patterns stored in the properties of a NumberFormatInfo. To modify how a value is displayed, the NumberFormatInfo must be writable so custom patterns can be saved in its properties.

The following table lists the standard format characters for each standard pattern and the associated NumberFormatInfo property that can be set to modify the standard pattern.

Format Character

Description and Associated Properties

c, C

Currency format. 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. NumberNegativePattern, NumberGroupSizes, NumberGroupSeparator, NumberDecimalDigits, NumberDecimalSeparator.

r, R

Roundtrip format, which ensures that numbers converted to strings will have the same value when they are converted back to numbers.

x, X

Hexadecimal format.

A DateTimeFormatInfo or a NumberFormatInfo 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.

This class implements the ICloneable interface to enable duplication of NumberFormatInfo objects. It also implements IFormatProvider to supply formatting information to applications.

The following code example shows how to get a NumberFormatInfo for a corresponding CultureInfo and use that NumberFormatInfo to query number formatting information for that 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'

System.Object
  System.Globalization.NumberFormatInfo

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft