[Serializable] |
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 NumberFormatInfo.CurrentInfo property. To create a NumberFormatInfo for the invariant culture, use the NumberFormatInfo.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 CultureInfo.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. NumberFormatInfo.CurrencyNegativePattern , NumberFormatInfo.CurrencyPositivePattern , NumberFormatInfo.CurrencySymbol , NumberFormatInfo.CurrencyGroupSizes , NumberFormatInfo.CurrencyGroupSeparator , NumberFormatInfo.CurrencyDecimalDigits , NumberFormatInfo.CurrencyDecimalSeparator . |
d, D | Decimal format. |
e, E | Scientific (exponential) format. |
f, F | Fixed-point format. |
g, G | General format. |
n, N | Number format. NumberFormatInfo.NumberNegativePattern , NumberFormatInfo.NumberGroupSizes , NumberFormatInfo.NumberGroupSeparator , NumberFormatInfo.NumberDecimalDigits , NumberFormatInfo.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.
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. Initializes a new writable instance of the NumberFormatInfo class that is culture-independent (invariant). |
CurrencyDecimalDigits | Read-write Indicates the number of decimal places to use in currency values. |
CurrencyDecimalSeparator | Read-write Gets or sets the string to use as the decimal separator in currency values. |
CurrencyGroupSeparator | Read-write Gets or sets the string that separates groups of digits to the left of the decimal in currency values. |
CurrencyGroupSizes | Read-write Gets or sets the number of digits in each group to the left of the decimal in currency values. |
CurrencyNegativePattern | Read-write Gets or sets the format pattern for negative currency values. |
CurrencyPositivePattern | Read-write Gets or sets the format pattern for positive currency values. |
CurrencySymbol | Read-write Gets or sets the string to use as the currency symbol. |
CurrentInfo | Read-only Gets a read-only NumberFormatInfo that formats values based on the current culture. |
InvariantInfo | Read-only Gets the default read-only NumberFormatInfo that is culture-independent (invariant). |
IsReadOnly | Read-only Gets a value indicating whether the NumberFormatInfo is read-only. |
NaNSymbol | Read-write Gets or sets the string that represents the IEEE NaN (not a number) value. |
NegativeInfinitySymbol | Read-write Gets or sets the string that represents negative infinity. |
NegativeSign | Read-write Gets or sets the string that denotes that the associated number is negative. |
NumberDecimalDigits | Read-write Gets or sets the number of decimal places to use in numeric values. |
NumberDecimalSeparator | Read-write Gets or sets the string to use as the decimal separator in numeric values. |
NumberGroupSeparator | Read-write Gets or sets the string that separates groups of digits to the left of the decimal in numeric values. |
NumberGroupSizes | Read-write Gets or sets the number of digits in each group to the left of the decimal in numeric values. |
NumberNegativePattern | Read-write Gets or sets the format pattern for negative numeric values. |
PercentDecimalDigits | Read-write Gets or sets the number of decimal places to use in percent values. |
PercentDecimalSeparator | Read-write Gets or sets the string to use as the decimal separator in percent values. |
PercentGroupSeparator | Read-write Gets or sets the string that separates groups of digits to the left of the decimal in percent values. |
PercentGroupSizes | Read-write Gets or sets the number of digits in each group to the left of the decimal in percent values. |
PercentNegativePattern | Read-write Gets or sets the format pattern for negative percent values. |
PercentPositivePattern | Read-write Gets or sets the format pattern for positive percent values. |
PercentSymbol | Read-write Gets or sets the string to use as the percent symbol. |
PerMilleSymbol | Read-write Gets or sets the string to use as the per mille symbol. |
PositiveInfinitySymbol | Read-write Gets or sets the string that represents positive infinity. |
PositiveSign | Read-write Gets or sets the string that denotes that the associated number is positive. |
Clone | Creates a shallow copy of the NumberFormatInfo. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetFormat | Gets an object of the specified type that provides a number formatting service. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetInstance | Gets the NumberFormatInfo associated with the specified IFormatProvider. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
ReadOnly | Returns a read-only NumberFormatInfo wrapper. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public NumberFormatInfo(); |
public int CurrencyDecimalDigits {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The property is being set to a value that is less than 0 or greater than 99. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string CurrencyDecimalSeparator {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
ArgumentException | The property is being set to a value that is the same as NumberFormatInfo.NumberGroupSeparator, NumberFormatInfo.CurrencyGroupSeparator, or NumberFormatInfo.PercentGroupSeparator. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string CurrencyGroupSeparator {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
ArgumentException | The property is being set to a value that is the same as NumberFormatInfo.NumberDecimalSeparator, NumberFormatInfo.CurrencyDecimalSeparator, or NumberFormatInfo.PercentDecimalSeparator. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public int[] CurrencyGroupSizes {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | The property is being set and the array contains an entry that is less than 0 or greater than 9. -or- The property is being set and the array contains an entry, other than the last entry, that is set to 0. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
The first element of the array defines the number of elements in the least significant group of digits immediately to the left of the NumberFormatInfo.CurrencyDecimalSeparator. Each subsequent element refers to the next significant group of digits to the left of the previous group. If the last element of the array is not 0, the remaining digits are grouped based on the last element of the array. If the last element is 0, the remaining digits are not grouped.
For example, if the array contains { 3, 4, 5 }, the digits will be grouped similar to "$55,55555,55555,55555,4444,333.00". If the array contains { 3, 4, 0 }, the digits will be grouped similar to "$55555555555555555,4444,333.00".
public int CurrencyNegativePattern {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The property is being set to a value that is less than 0 or greater than 15. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
Value | Associated Pattern |
---|---|
0 | ($n) |
1 | -$n |
2 | $-n |
3 | $n- |
4 | (n$) |
5 | -n$ |
6 | n-$ |
7 | n$- |
8 | -n $ |
9 | -$ n |
10 | n $- |
11 | $ n- |
12 | $ -n |
13 | n- $ |
14 | ($ n) |
15 | (n $) |
public int CurrencyPositivePattern {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The property is being set to a value that is less than 0 or greater than 3. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
Value | Associated Pattern |
---|---|
0 | $n |
1 | n$ |
2 | $ n |
3 | n $ |
public string CurrencySymbol {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public static NumberFormatInfo CurrentInfo {get;}
|
public static NumberFormatInfo InvariantInfo {get;}
|
using System; using System.Globalization; using System.Text; class SamplesNumberFormatInfo { public static void Main() { // Gets the InvariantInfo. NumberFormatInfo myInv = NumberFormatInfo.InvariantInfo; // Gets a UnicodeEncoding to display the Unicode value of symbols. UnicodeEncoding myUE = new UnicodeEncoding( true, false ); byte[] myCodes; // Displays the default values for each of the properties. Console.WriteLine( "InvariantInfo:\nNote: Symbols may not display correctly on the console,\ntherefore, Unicode values are included." ); Console.WriteLine( "\tCurrencyDecimalDigits\t\t{0}", myInv.CurrencyDecimalDigits ); Console.WriteLine( "\tCurrencyDecimalSeparator\t{0}", myInv.CurrencyDecimalSeparator ); Console.WriteLine( "\tCurrencyGroupSeparator\t\t{0}", myInv.CurrencyGroupSeparator ); Console.WriteLine( "\tCurrencyGroupSizes\t\t{0}", myInv.CurrencyGroupSizes[0] ); Console.WriteLine( "\tCurrencyNegativePattern\t\t{0}", myInv.CurrencyNegativePattern ); Console.WriteLine( "\tCurrencyPositivePattern\t\t{0}", myInv.CurrencyPositivePattern ); myCodes = myUE.GetBytes( myInv.CurrencySymbol ); Console.WriteLine( "\tCurrencySymbol\t\t\t{0}\t(U+{1:x2}{2:x2})", myInv.CurrencySymbol, myCodes[0], myCodes[1] ); Console.WriteLine( "\tNaNSymbol\t\t\t{0}", myInv.NaNSymbol ); Console.WriteLine( "\tNegativeInfinitySymbol\t\t{0}", myInv.NegativeInfinitySymbol ); Console.WriteLine( "\tNegativeSign\t\t\t{0}", myInv.NegativeSign ); Console.WriteLine( "\tNumberDecimalDigits\t\t{0}", myInv.NumberDecimalDigits ); Console.WriteLine( "\tNumberDecimalSeparator\t\t{0}", myInv.NumberDecimalSeparator ); Console.WriteLine( "\tNumberGroupSeparator\t\t{0}", myInv.NumberGroupSeparator ); Console.WriteLine( "\tNumberGroupSizes\t\t{0}", myInv.NumberGroupSizes[0] ); Console.WriteLine( "\tNumberNegativePattern\t\t{0}", myInv.NumberNegativePattern ); Console.WriteLine( "\tPercentDecimalDigits\t\t{0}", myInv.PercentDecimalDigits ); Console.WriteLine( "\tPercentDecimalSeparator\t\t{0}", myInv.PercentDecimalSeparator ); Console.WriteLine( "\tPercentGroupSeparator\t\t{0}", myInv.PercentGroupSeparator ); Console.WriteLine( "\tPercentGroupSizes\t\t{0}", myInv.PercentGroupSizes[0] ); Console.WriteLine( "\tPercentNegativePattern\t\t{0}", myInv.PercentNegativePattern ); Console.WriteLine( "\tPercentPositivePattern\t\t{0}", myInv.PercentPositivePattern ); myCodes = myUE.GetBytes( myInv.PercentSymbol ); Console.WriteLine( "\tPercentSymbol\t\t\t{0}\t(U+{1:x2}{2:x2})", myInv.PercentSymbol, myCodes[0], myCodes[1] ); myCodes = myUE.GetBytes( myInv.PerMilleSymbol ); Console.WriteLine( "\tPerMilleSymbol\t\t\t{0}\t(U+{1:x2}{2:x2})", myInv.PerMilleSymbol, myCodes[0], myCodes[1] ); Console.WriteLine( "\tPositiveInfinitySymbol\t\t{0}", myInv.PositiveInfinitySymbol ); Console.WriteLine( "\tPositiveSign\t\t\t{0}", myInv.PositiveSign ); } } /* This code produces the following output. InvariantInfo: Note: Symbols may not display correctly on the console, therefore, Unicode values are included. CurrencyDecimalDigits 2 CurrencyDecimalSeparator . CurrencyGroupSeparator , CurrencyGroupSizes 3 CurrencyNegativePattern 0 CurrencyPositivePattern 0 CurrencySymbol (U+00a4) NaNSymbol NaN NegativeInfinitySymbol -Infinity NegativeSign - NumberDecimalDigits 2 NumberDecimalSeparator . NumberGroupSeparator , NumberGroupSizes 3 NumberNegativePattern 1 PercentDecimalDigits 2 PercentDecimalSeparator . PercentGroupSeparator , PercentGroupSizes 3 PercentNegativePattern 0 PercentPositivePattern 0 PercentSymbol % (U+0025) PerMilleSymbol % (U+2030) PositiveInfinitySymbol Infinity PositiveSign + */
public bool IsReadOnly {get;}
|
public string NaNSymbol {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string NegativeInfinitySymbol {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string NegativeSign {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public int NumberDecimalDigits {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The property is being set to a value that is less than 0 or greater than 99. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string NumberDecimalSeparator {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
ArgumentException | The property is being set to a value that is the same as NumberFormatInfo.NumberGroupSeparator, NumberFormatInfo.CurrencyGroupSeparator, or NumberFormatInfo.PercentGroupSeparator. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string NumberGroupSeparator {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
ArgumentException | The property is being set to a value that is the same as NumberFormatInfo.NumberDecimalSeparator, NumberFormatInfo.CurrencyDecimalSeparator, or NumberFormatInfo.PercentDecimalSeparator. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public int[] NumberGroupSizes {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | The property is being set and the array contains an entry that is less than 0 or greater than 9. -or- The property is being set and the array contains an entry, other than the last entry, that is set to 0. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
The first element of the array defines the number of elements in the least significant group of digits immediately to the left of the NumberFormatInfo.NumberDecimalSeparator. Each subsequent element refers to the next significant group of digits to the left of the previous group. If the last element of the array is not 0, the remaining digits are grouped based on the last element of the array. If the last element is 0, the remaining digits are not grouped.
For example, if the array contains { 3, 4, 5 }, the digits will be grouped similar to "55,55555,55555,55555,4444,333.00". If the array contains { 3, 4, 0 }, the digits will be grouped similar to "55555555555555555,4444,333.00".
using System; using System.Globalization; class SamplesNumberFormatInfo { public static void Main() { // Creates a new NumberFormatinfo. NumberFormatInfo myNfi = new NumberFormatInfo(); // Takes a long value. Int64 myInt = 123456789012345; // Displays the value with default formatting. Console.WriteLine( "Default \t\t:\t{0}", myInt.ToString(myNfi) ); // Displays the value with three elements in the GroupSize array. myNfi.NumberGroupSizes = new int[] { 2, 3, 4 }; Console.WriteLine( "Grouping ( {0} )\t:\t{1}", PrintArraySet( myNfi.NumberGroupSizes ), myInt.ToString(myNfi) ); // Displays the value with zero as the last element in the GroupSize array. myNfi.NumberGroupSizes = new int[] { 2, 4, 0 }; Console.WriteLine( "Grouping ( {0} )\t:\t{1}", PrintArraySet( myNfi.NumberGroupSizes ), myInt.ToString(myNfi) ); } public static string PrintArraySet( int[] myArr ) { string myStr = null; myStr = myArr[0].ToString(); for ( int i = 1; i < myArr.Length; i++ ) myStr += ", " + myArr[i].ToString(); return( myStr ); } } /* This code produces the following output. Default : 123,456,789,012,345.00 Grouping ( 2, 3, 4 ) : 12,3456,7890,123,45.00 Grouping ( 2, 4, 0 ) : 123456789,0123,45.00 */
public int NumberNegativePattern {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The property is being set to a value that is less than 0 or greater than 4. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
Value | Associated Pattern |
---|---|
0 | (n) |
1 | -n |
2 | - n |
3 | n- |
4 | n - |
using System; using System.Globalization; class SamplesNumberFormatInfo { public static void Main() { // Creates a new NumberFormatinfo. NumberFormatInfo myNfi = new NumberFormatInfo(); // Takes a negative value. Int64 myInt = -1234; // Displays the value with default formatting. Console.WriteLine( "Default \t:\t{0}", myInt.ToString( "N", myNfi ) ); // Displays the value with other patterns. for ( int i = 0; i <= 4; i++ ) { myNfi.NumberNegativePattern = i; Console.WriteLine( "Pattern {0}\t:\t{1}", myNfi.NumberNegativePattern, myInt.ToString( "N", myNfi ) ); } } } /* This code produces the following output. Default : (1,234.00) Pattern 0 : (1,234.00) Pattern 1 : -1,234.00 Pattern 2 : - 1,234.00 Pattern 3 : 1,234.00- Pattern 4 : 1,234.00 - */
public int PercentDecimalDigits {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The property is being set to a value that is less than 0 or greater than 99. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string PercentDecimalSeparator {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
ArgumentException | The property is being set to a value that is the same as NumberFormatInfo.NumberGroupSeparator, NumberFormatInfo.CurrencyGroupSeparator, or NumberFormatInfo.PercentGroupSeparator. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string PercentGroupSeparator {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
ArgumentException | The property is being set to a value that is the same as NumberFormatInfo.NumberDecimalSeparator, NumberFormatInfo.CurrencyDecimalSeparator, or NumberFormatInfo.PercentDecimalSeparator. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public int[] PercentGroupSizes {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | The property is being set and the array contains an entry that is less than 0 or greater than 9. -or- The property is being set and the array contains an entry, other than the last entry, that is set to 0. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
The first element of the array defines the number of elements in the least significant group of digits immediately to the left of the NumberFormatInfo.PercentDecimalSeparator. Each subsequent element refers to the next significant group of digits to the left of the previous group. If the last element of the array is not 0, the remaining digits are grouped based on the last element of the array. If the last element is 0, the remaining digits are not grouped.
For example, if the array contains { 3, 4, 5 }, the digits will be grouped similar to "55,55555,55555,55555,4444,333.00%". If the array contains { 3, 4, 0 }, the digits will be grouped similar to "55555555555555555,4444,333.00%".
public int PercentNegativePattern {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The property is being set to a value that is less than 0 or greater than 2. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
Value | Associated Pattern |
---|---|
0 | -n % |
1 | -n% |
2 | -%n |
public int PercentPositivePattern {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The property is being set to a value that is less than 0 or greater than 2. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
Value | Associated Pattern |
---|---|
0 | n % |
1 | n% |
2 | %n |
public string PercentSymbol {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string PerMilleSymbol {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string PositiveInfinitySymbol {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property is being set to null. |
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public string PositiveSign {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The property is being set and the NumberFormatInfo is read-only. |
public object Clone(); |
A shallow copy of an object is a copy of the object only. If the object contains references to other objects, the shallow copy will not create copies of the referred objects. It will refer to the original objects instead. On the other hand, a deep copy of an object creates a copy of the object and a copy of everything directly or indirectly referenced by that object.
~NumberFormatInfo(); |
formatType
public virtual int GetHashCode(); |
public static NumberFormatInfo GetInstance( |
formatProvider
-or-
null to get NumberFormatInfo.CurrentInfo.
The IFormatProvider used to get the NumberFormatInfo.-or-
null to get NumberFormatInfo.CurrentInfo.
You can get a NumberFormatInfo for a specific culture using one of the following methods:
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.
public Type GetType(); |
protected object MemberwiseClone(); |
public static NumberFormatInfo ReadOnly( |
nfi
Exception Type | Condition |
---|---|
ArgumentNullException | nfi is null. |
Attempting to perform an assignment to a property of a read-only NumberFormatInfo causes an InvalidOperationException.
public virtual string ToString(); |