| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Define and Use Custom Numeric Format Providers |
03/30/2017 |
.net |
dotnet-standard |
article |
|
a281bfbf-6596-45ed-a2d6-3782d535ada2 |
11 |
rpetrusha |
ronpet |
wpickett |
How to: Define and Use Custom Numeric Format Providers
The [!INCLUDEdnprdnshort] gives you extensive control over the string representation of numeric values. It supports the following features for customizing the format of numeric values:
-
Standard numeric format strings, which provide a predefined set of formats for converting numbers to their string representation. You can use them with any numeric formatting method, such as xref:System.Decimal.ToString%28System.String%29?displayProperty=nameWithType, that has a
formatparameter. For details, see Standard Numeric Format Strings. -
Custom numeric format strings, which provide a set of symbols that can be combined to define custom numeric format specifiers. They can also be used with any numeric formatting method, such as xref:System.Decimal.ToString%28System.String%29?displayProperty=nameWithType, that has a
formatparameter. For details, see Custom Numeric Format Strings. -
Custom xref:System.Globalization.CultureInfo or xref:System.Globalization.NumberFormatInfo objects, which define the symbols and format patterns used in displaying the string representations of numeric values. You can use them with any numeric formatting method, such as xref:System.Int32.ToString%2A, that has a
providerparameter. Typically, theproviderparameter is used to specify culture-specific formatting.
In some cases (such as when an application must display a formatted account number, an identification number, or a postal code) these three techniques are inappropriate. The [!INCLUDEdnprdnshort] also enables you to define a formatting object that is neither a xref:System.Globalization.CultureInfo nor a xref:System.Globalization.NumberFormatInfo object to determine how a numeric value is formatted. This topic provides the step-by-step instructions for implementing such an object, and provides an example that formats telephone numbers.
To define a custom format provider
-
Define a class that implements the xref:System.IFormatProvider and xref:System.ICustomFormatter interfaces.
-
Implement the xref:System.IFormatProvider.GetFormat%2A?displayProperty=nameWithType method. xref:System.IFormatProvider.GetFormat%2A is a callback method that the formatting method (such as the xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType method) invokes to retrieve the object that is actually responsible for performing custom formatting. A typical implementation of xref:System.IFormatProvider.GetFormat%2A does the following:
-
Determines whether the xref:System.Type object passed as a method parameter represents an xref:System.ICustomFormatter interface.
-
If the parameter does represent the xref:System.ICustomFormatter interface, xref:System.IFormatProvider.GetFormat%2A returns an object that implements the xref:System.ICustomFormatter interface that is responsible for providing custom formatting. Typically, the custom formatting object returns itself.
-
If the parameter does not represent the xref:System.ICustomFormatter interface, xref:System.IFormatProvider.GetFormat%2A returns
null.
-
-
Implement the xref:System.ICustomFormatter.Format%2A method. This method is called by the xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType method and is responsible for returning the string representation of a number. Implementing the method typically involves the following:
-
Optionally, make sure that the method is legitimately intended to provide formatting services by examining the
providerparameter. For formatting objects that implement both xref:System.IFormatProvider and xref:System.ICustomFormatter, this involves testing theproviderparameter for equality with the current formatting object. -
Determine whether the formatting object should support custom format specifiers. (For example, an "N" format specifier might indicate that a U.S. telephone number should be output in NANP format, and an "I" might indicate output in ITU-T Recommendation E.123 format.) If format specifiers are used, the method should handle the specific format specifier. It is passed to the method in the
formatparameter. If no specifier is present, the value of theformatparameter is xref:System.String.Empty?displayProperty=nameWithType. -
Retrieve the numeric value passed to the method as the
argparameter. Perform whatever manipulations are required to convert it to its string representation. -
Return the string representation of the
argparameter.
-
To use a custom numeric formatting object
-
Create a new instance of the custom formatting class.
-
Call the xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType formatting method, passing it the custom formatting object, the formatting specifier (or xref:System.String.Empty?displayProperty=nameWithType, if one is not used), and the numeric value to be formatted.
Example
The following example defines a custom numeric format provider named TelephoneFormatter that converts a number that represents a U.S. telephone number to its NANP or E.123 format. The method handles two format specifiers, "N" (which outputs the NANP format) and "I" (which outputs the international E.123 format).
[!code-csharpFormatting.HowTo.NumericValue#1] [!code-vbFormatting.HowTo.NumericValue#1]
The custom numeric format provider can be used only with the xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType method. The other overloads of numeric formatting methods (such as ToString) that have a parameter of type xref:System.IFormatProvider all pass the xref:System.IFormatProvider.GetFormat%2A?displayProperty=nameWithType implementation a xref:System.Type object that represents the xref:System.Globalization.NumberFormatInfo type. In return, they expect the method to return a xref:System.Globalization.NumberFormatInfo object. If it does not, the custom numeric format provider is ignored, and the xref:System.Globalization.NumberFormatInfo object for the current culture is used in its place. In the example, the TelephoneFormatter.GetFormat method handles the possibility that it may be inappropriately passed to a numeric formatting method by examining the method parameter and returning null if it represents a type other than xref:System.ICustomFormatter.
If a custom numeric format provider supports a set of format specifiers, make sure you provide a default behavior if no format specifier is supplied in the format item used in the xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType method call. In the example, "N" is the default format specifier. This allows for a number to be converted to a formatted telephone number by providing an explicit format specifier. The following example illustrates such a method call.
[!code-csharpFormatting.HowTo.NumericValue#2] [!code-vbFormatting.HowTo.NumericValue#2]
But it also allows the conversion to occur if no format specifier is present. The following example illustrates such a method call.
[!code-csharpFormatting.HowTo.NumericValue#3] [!code-vbFormatting.HowTo.NumericValue#3]
If no default format specifier is defined, your implementation of the xref:System.ICustomFormatter.Format%2A?displayProperty=nameWithType method should include code such as the following so that .NET can provide formatting that your code does not support.
[!code-csharpSystem.ICustomFormatter.Format#1] [!code-vbSystem.ICustomFormatter.Format#1]
In the case of this example, the method that implements xref:System.ICustomFormatter.Format%2A?displayProperty=nameWithType is intended to serve as a callback method for the xref:System.String.Format%28System.IFormatProvider%2CSystem.String%2CSystem.Object%5B%5D%29?displayProperty=nameWithType method. Therefore, it examines the formatProvider parameter to determine whether it contains a reference to the current TelephoneFormatter object. However, the method can also be called directly from code. In that case, you can use the formatProvider parameter to provide a xref:System.Globalization.CultureInfo or xref:System.Globalization.NumberFormatInfo object that supplies culture-specific formatting information.
Compiling the Code
Compile the code at the command line using csc.exe or vb.exe. To compile the code in [!INCLUDEvsprvs], put it in a console application project template.