For platform: Microsoft .net 1.1 and higher.
AdvancedFormatProvider is an IFormatProvider which means it works with String.Format() and other consumers of this interface. For example:
text =
String.Format(AdvancedFormatProvider.Current, "{0:C-$}", 100.0)
It supports all existing formats supplied by the .net framework, plus those included with this product and you create.
AdvancedFormatProvider makes it easy to drop in new
formatters, tools that convert a value into a string based on a format string
in the {#:here}
token. (For
example, {0:c}
and {1:yyyy-MM-dd}
.)
The formatters are registered globally so you only install them once, in Application_Start.
It pre-installs support for these formatters:
-
IntegerFormatter - For use with integers, or to
format decimals like integers. Uses the symbol "i" or
"I" such as
{0:I}
and{0:i}
. It is similar to theN0
formatter built into .net, except it has an option to omit group separators.<p style='margin-left:.5in'> The symbol can be followed with these modifiers: </p> <ul type='circle'> <li> <code>{0:I-,}</code> - Omit the group separator character. When not present, the group separator is used. </li> <li> <code>{0:I,}</code> - Include the group separator. Not necessary because when there is no comma, it includes the group separator. </li> <li> <code>{0:I#}</code> - The number of digits to show where # is shown. If the value is less than this, lead zeros are used to fill. </li> </ul> <p style='margin-left:.5in'> <i>Examples: </i> </p> <ul type='circle'> <li> <code>{0:I}</code>, <code>{0:I,}</code> 10000 -> "10,000" (includes group separators) </li> <li > <code>{0:I3}</code>, <code>{0:I,3} </code> 1 -> "001" (includes group separators and fills to 3 digits) </li> <li> <code>{0:I-,}</code> 10000 -> "10000" (omits group separators) </li> <li> <code>{0:I-,7}</code> 11234 -> "0011234" (omits group separators and fills to 8 digits) </li> </ul>
-
CurrencyAndPercentFormatter - For use with numbers
to format them either as a currency or percent. This overrides the native
formatters, using the same format strings ("c", "C",
"p", "P"), adding rules to omit the group separator,
currency or percent symbol, or decimal digits when all zero.
The initial character can be followed with these modifiers:
-
<code>{0:C-$}</code> - Omit the currency symbol. When not present, the currency symbol is used. </li> <li> <code>{0:C$}</code> - Include the currency symbol. Not necessary because when there is no $, it means include the currency symbol </li> <li> <code>{0:P-%}</code> - Omit the percent symbol. When not present, the percent symbol is used. </li> <li> <code>{0:P%}</code> - Include the percent symbol. Not necessary because when there is no %, it means include the percent symbol </li> <li> <code>{0:C-,}</code> or <code>{0:P-,}</code> - Omit the group separator character. When not present, the group separator is used. </li> <li> <code>{0:C,}</code> or <code>{0:P,}</code> - Include the group separator. Not necessary because when there is no comma, it includes the group separator. </li> <li> <code>{0:C-0}</code> or <code>{0:P-0}</code> - Omit the floating point part when its zero (the value would be the same as an integer). </li> <li> <code>{0:C#}</code> or <code>{0:P#}</code> - The number of floating point digits to show where # is shown. If not specified, it uses the culture's rule. Use 0 to remove the floating point part. However, if not specified and the value is already an integer, the floating point part is always removed. </li> </ul> <p style='margin-left:.5in'> <i>Examples for currency:</i> </p> <ul type='circle'> <li> <code>{0:C}</code>, <code>{0:C,}</code> 5.0 -> "$5.00" (includes currency symbol and group separators)<br /> <p style='margin-left:2.0in;text-indent:.5in'> 5000.0 -> "$5,000.00" </p> <p style='margin-left:4.15in;text-indent:-3.4in'> 50 -> "$50" (because it is an integer passed in) </p> </li> <li> <code>{0:C-$}</code> 5.0 -> "5.00" (omits currency symbol; includes group separators)<br /> <p style='margin-left:2.0in;text-indent:.5in'> 5000.0 -> "5,000.00" </p> </li> <li> <code>{0:C-$-,}</code> 5000.0 -> "5000.00" (omits currency symbol and group separators) </li> <li> <code>{0:C3}</code>, <code>{0:C,3}</code> 5.00 -> "$5.000" (includes group separators and requires 3 decimal digits) </li> <li> <code>{0:C0}</code>, <code>{0:C,0}</code> 50.0 -> "$50" (includes group separators and removes decimal digits), </li> <li> <code>{0:C-,}</code> 5000.0 -> "$5000.00" (omits group separators) </li> <li> <code>{0:C-,3}</code> 5000.0 -> "$5000.000" (omits group separators and requires 3 decimal digits) </li> <li> <code>{0:C-,0}</code> 5000.0 -> "$5000" (omits group separators and removes decimal digits) </li> <li> <code>{0:C-0}</code> 5000.00 -> "$5,000" (removes decimal part when zero; includes currency symbol and group separators)<br /> <p style='margin-left:2.0in;text-indent:.5in'> 5000.01 -> "$5,000.01" </p> </li> <li> <code>{0:C-$-,-0}</code> 5000.0 -> "5000" (removes decimal part when zero; omits currency symbol and group separators)<br /> <p style='margin-left:2.0in;text-indent:.5in'> 5000.01 -> "5000.01" </p> </li> </ul> <p style='margin-left:.5in'> <i>Examples for percent:</i> </p> <ul type='circle'> <li> <code>{0:P}</code>, <code>{0:P,}</code> 0.50 -> "50.00 %" (includes percent symbol and group separators)<br/> <p style='margin-left:2.0in;text-indent:.5in'> 50.0 -> "5,000.00 %" </p> <p style='margin-left:2.0in;text-indent:.5in'> 50 -> "5,000 %" (because it is an integer passed in) </p> </li> <li> <code>{0:P-%}</code> 0.50 -> "50.00" (omits percent symbol; includes group separators)<br /> <p style='margin-left:2.0in;text-indent:.5in'> 50.0 -> "5,000.00" </p> </li> <li> <code>{0:P-%-,}</code> 50.0 -> "5000.00" (omits percent symbol and group separators) </li> <li> <code>{0:P3}</code>, <code>{0:P,3}</code> 0.50 -> "50.000 %" (includes group separators and requires 3 decimal digits) </li> <li> <code>{0:P0}</code>, <code>{0:P,0}</code> 50.0 -> "$5,000" (includes group separators and removes decimal digits) </li> <li> <code>{0:P-,}</code> 50.0 -> "5000.0 %" (omits group separators) </li> <li> <code>{0:P-,3}</code> 50.0 -> "5000.000 %" (omits group separators and requires 3 decimal digits), </li> <li> <code>{0:P-,0}</code> 50.0 -> "5000 %" (omits group separators and removes decimal digits) </li> <li> <code>{0:P-0}</code> 0.50 -> "50 %" (removes decimal part when zero; includes percent symbol and group separators)<br /> <p> 0.501 -> "50.1 %" </p> </li> <li> <code>{0:P-%-,-0}</code> 0.50 -> "50" (removes decimal part when zero; omits percent symbol and group separators)<br /> <p> 0.501 -> "50.1" </p> </li> </ul>
Add more by creating a class that implements the IAdvancedFormatterPlugIn interface.
Either add the Visual Studio project into your application or add the source code files.
After adding the project, add a reference from your application to the project.
The source code files are in C#. If your application is not in C#, use the Visual Studio project file.
Add the source code files to your application. The source code files do not specify a namespace. Consider adding your own application’s namespace to them.
You can either call String.Format() passing in the AdvancedFormatProvider as the first parameter, or call AdvancedFormatProvider.Format(), with similar parameters to String.Format. AdvancedFormatProvider.Format() lets you avoid creating an instance of AdvancedFormatProvider and makes it easy to pass in a specific System.Globalization.CultureInfo object.
text = String.Format(AdvancedFormatProvider.Current, " text with tokens ", value1, value2, etc.)
text = AdvancedFormatProvider.Format("text with tokens", value1, value2, etc.)
Examples
text = String.Format(AdvancedFormatProvider.Current, "The {0} has a value of {1:C}.", "book", 10.0)
text = AdvancedFormatProvider.Format ("The {0} has a value of {1:C}." , "book", 10.0
Result:
"The book has a value of $10.00."
text = String.Format(new AdvancedFormatProvider(cultureInfo), "text with tokens", value1, value2, etc.)
text = AdvancedFormatProvider.Format(cultureInfo, "text with tokens" , value1, value2, etc.)
Examples
text = String.Format(AdvancedFormatProvider.Current, "The {0} has a value of {1:C}." , "book", 10.0)
text = AdvancedFormatProvider.Format( "The {0} has a value of {1:C}." , "book", 10.0
Result:
"The book has a value of $10.00."
Create a class that implements the IAdvancedFormatterPlugIn interface. See the code in the IAdvancedFormatProvider.cs source code file.
To add your class to AdvancedFormatProvider, in application startup code, call
AdvancedFormatProvider.RegisterFormatPlugIn(new YourClass())
passing an instance of your class. Your object will be maintained globally.Do not attempt to modify it after application startup.
-