Defines how numeric values are formatted and displayed, depending on the culture.
Namespace:
System.Globalization
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public NotInheritable Class NumberFormatInfo _
Implements IFormatProvider
Dim instance As NumberFormatInfo
[ComVisibleAttribute(true)]
public sealed class NumberFormatInfo : IFormatProvider
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 code 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$'
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.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference