Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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)
[ComVisibleAttribute(true)]
public sealed class NumberFormatInfo : IFormatProvider

The NumberFormatInfo type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360NumberFormatInfoInitializes a new writable instance of the NumberFormatInfo class that is culture-independent (invariant).
Top
  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360CurrencyDecimalDigitsGets or sets the number of decimal places to use in currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360CurrencyDecimalSeparatorGets or sets the string to use as the decimal separator in currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360CurrencyGroupSeparatorGets 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 360CurrencyGroupSizesGets 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 360CurrencyNegativePatternGets or sets the format pattern for negative currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360CurrencyPositivePatternGets or sets the format pattern for positive currency values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360CurrencySymbolGets or sets the string to use as the currency symbol.
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360CurrentInfoGets a read-only NumberFormatInfo object that contains numeric formatting information for the current culture.
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360InvariantInfoGets the default read-only NumberFormatInfo object that is culture-independent (invariant).
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsReadOnlyGets a value indicating whether the NumberFormatInfo object is read-only.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NaNSymbolGets or sets the string that represents the IEEE NaN (not a number) value.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NegativeInfinitySymbolGets or sets the string that represents negative infinity.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NegativeSignGets or sets the string that indicates that a number is negative.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NumberDecimalDigitsGets or sets the number of decimal places to use in numeric values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NumberDecimalSeparatorGets or sets the string to use as the decimal separator in numeric values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NumberGroupSeparatorGets 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 360NumberGroupSizesGets 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 360NumberNegativePatternGets or sets the format pattern for negative numeric values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360PercentDecimalDigitsGets or sets the number of decimal places to use in percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360PercentDecimalSeparatorGets or sets the string to use as the decimal separator in percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360PercentGroupSeparatorGets 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 360PercentGroupSizesGets 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 360PercentNegativePatternGets or sets the format pattern for negative percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360PercentPositivePatternGets or sets the format pattern for positive percent values.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360PercentSymbolGets or sets the string to use as the percent symbol.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360PerMilleSymbolGets or sets the string to use as the per mille symbol.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360PositiveInfinitySymbolGets or sets the string that represents positive infinity.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360PositiveSignGets or sets the string that denotes that the associated number is positive.
Top
  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CloneCreates a shallow copy of the NumberFormatInfo object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows 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 360GetFormatGets an object of the specified type that provides a numeric formatting service.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetInstanceGets the NumberFormatInfo object associated with the specified IFormatProvider object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360ReadOnlyReturns a read-only NumberFormatInfo wrapper.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

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 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$'


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

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.