NumberFormatInfo Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

Public NotInheritable Class NumberFormatInfo _
    Implements IFormatProvider
public sealed class NumberFormatInfo : IFormatProvider

The NumberFormatInfo type exposes the following members.

Constructors

  Name Description
NumberFormatInfo Initializes a new writable instance of the NumberFormatInfo class that is culture-independent (invariant).

Top

Properties

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

Top

Methods

  Name Description
Clone Creates a shallow copy of the NumberFormatInfo object.
Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
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.)
GetFormat Gets an object of the specified type that provides a numeric formatting service.
GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
GetInstance Gets the NumberFormatInfo object associated with the specified IFormatProvider object.
GetType Gets the Type of the current instance. (Inherited from Object.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
ReadOnly Returns a read-only NumberFormatInfo wrapper.
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.

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

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

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.

See Also

Reference

System.Globalization Namespace

Decimal

CultureInfo