NumberFormatInfo Class
Overview
The NumberFormatInfo
class defines how numeric values are formatted and displayed, depending on culture-specific settings such as decimal separators, group separators, and currency symbols.
Properties
Property | Type | Description |
---|---|---|
CurrencyDecimalDigits | int | Number of decimal places to use in currency values. |
CurrencyDecimalSeparator | string | String to use as the decimal separator in currency values. |
CurrencyGroupSeparator | string | String to use as the group separator in currency values. |
CurrencySymbol | string | Symbol used to denote currency. |
NumberDecimalDigits | int | Number of decimal places to use in numeric values. |
NumberDecimalSeparator | string | String to use as the decimal separator in numeric values. |
NumberGroupSeparator | string | String to use as the group separator in numeric values. |
PercentDecimalDigits | int | Number of decimal places to use in percent values. |
PercentSymbol | string | Symbol used to denote percent. |
PositiveSign | string | String that denotes a positive sign. |
NegativeSign | string | String that denotes a negative sign. |
Methods
Method | Signature | Description |
---|---|---|
Clone | object Clone() | Creates a shallow copy of the current NumberFormatInfo object. |
GetInstance | static NumberFormatInfo GetInstance(IFormatProvider provider) | Returns the NumberFormatInfo associated with the specified provider. |
ReadOnly | static NumberFormatInfo ReadOnly(NumberFormatInfo nfi) | Returns a read-only wrapper for the specified NumberFormatInfo . |
Example
Format a number using custom NumberFormatInfo
settings:
using System;
using System.Globalization;
class Program
{
static void Main()
{
var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.CurrencySymbol = "€";
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = ".";
decimal value = 1234567.89m;
Console.WriteLine(value.ToString("N", nfi)); // 1.234.567,89
Console.WriteLine(value.ToString("C", nfi)); // €1.234.567,89
}
}
Remarks
NumberFormatInfo
is culture-sensitive. Use CultureInfo
to obtain standard formats for a specific culture, or clone and modify a NumberFormatInfo
instance for custom formatting.