Skip to content

plblum/AdvancedFormatProvider

Repository files navigation

AdvancedFormatProvider class and plug-ins

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 the N0 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 -&gt;
            &quot;10,000&quot;       (includes group separators)
         </li>
    
         <li >
            
            <code>{0:I3}</code>, <code>{0:I,3} </code> 1 -&gt;
            &quot;001&quot;                  (includes group separators and fills to 3
            digits)
         </li>
    
         <li>
            
            <code>{0:I-,}</code>            
                  10000 -&gt; &quot;10000&quot;        (omits group separators)
         </li>
    
         <li>
            
            <code>{0:I-,7}</code>           
                11234 -&gt; &quot;0011234&quot;    (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 -&gt;
              &quot;$5.00&quot;                 (includes currency symbol and group
              separators)<br />
              <p style='margin-left:2.0in;text-indent:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 5000.0 -&gt;
                 &quot;$5,000.00&quot;
              </p>
              <p style='margin-left:4.15in;text-indent:-3.4in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 50  
                 -&gt; &quot;$50&quot;                   (because it is an integer passed in)
              </p>
           </li>
      
           <li>
              
              <code>{0:C-$}</code>                   5.0 
              -&gt; &quot;5.00&quot;                  (omits currency symbol; includes group
              separators)<br />
              <p style='margin-left:2.0in;text-indent:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 5000.0 -&gt;
                 &quot;5,000.00&quot;
              </p>
           </li>
      
           <li>
              
              <code>{0:C-$-,}</code>              5000.0
              -&gt; &quot;5000.00&quot;       (omits currency symbol and group separators)
           </li>
      
           <li>
              
              <code>{0:C3}</code>, <code>{0:C,3}</code>  5.00 -&gt; &quot;$5.000&quot;             (includes
              group separators and requires 3 decimal digits)
           </li>
      
           <li>
              
              <code>{0:C0}</code>, <code>{0:C,0}</code>  50.0 -&gt; &quot;$50&quot;                  (includes
              group separators and removes decimal digits),
           </li>
      
           <li>
              
              <code>{0:C-,}</code>                   5000.0
              -&gt; &quot;$5000.00&quot;     (omits group separators)
           </li>
      
           <li>
              
              <code>{0:C-,3}</code>                5000.0
              -&gt; &quot;$5000.000&quot;   (omits group separators and requires 3 decimal
              digits)
           </li>
      
           <li>
              
              <code>{0:C-,0}</code>                5000.0
              -&gt; &quot;$5000&quot;          (omits group separators and removes decimal
              digits)
           </li>
      
           <li>
              
              <code>{0:C-0}</code>                   5000.00
              -&gt; &quot;$5,000&quot;       (removes decimal part when zero; includes
              currency symbol and group separators)<br />
              <p style='margin-left:2.0in;text-indent:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 5000.01 -&gt;
                 &quot;$5,000.01&quot;
              </p>
      
           </li>
      
           <li>
              
              <code>{0:C-$-,-0}</code>         5000.0
              -&gt; &quot;5000&quot;            (removes decimal part when zero; omits
              currency symbol and group separators)<br />
              <p style='margin-left:2.0in;text-indent:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 5000.01 -&gt;
                 &quot;5000.01&quot;
              </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 -&gt; &quot;50.00
              %&quot;           (includes percent symbol and group separators)<br/>
              <p style='margin-left:2.0in;text-indent:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 50.0 -&gt;
                 &quot;5,000.00 %&quot;
              </p>
      
              <p style='margin-left:2.0in;text-indent:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 50   -&gt;
                 &quot;5,000 %&quot;           (because it is an integer passed in)
              </p>
           </li>
      
      
           <li>
              
              <code>{0:P-%}</code>                   0.50
              -&gt; &quot;50.00&quot;               (omits percent symbol; includes group separators)<br />
              <p style='margin-left:2.0in;text-indent:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 50.0 -&gt;
                 &quot;5,000.00&quot;  
              </p>
      
           </li>
      
      
           <li>
              
              <code>{0:P-%-,}</code>              50.0
              -&gt; &quot;5000.00&quot;           (omits percent symbol and group separators)
      
           </li>
      
           <li>
              
              <code>{0:P3}</code>, <code>{0:P,3}</code>  0.50 -&gt; &quot;50.000
              %&quot;         (includes group separators and requires 3 decimal digits)
           </li>
      
           <li>
              
              <code>{0:P0}</code>, <code>{0:P,0}</code>  50.0 -&gt; &quot;$5,000&quot;             (includes
              group separators and removes decimal digits)
           </li>
      
           <li>
              
              <code>{0:P-,}</code>                50.0
              -&gt; &quot;5000.0 %&quot;         (omits group separators)
           </li>
      
           <li>
              
              <code>{0:P-,3}</code>                50.0
              -&gt; &quot;5000.000 %&quot;     (omits group separators and requires 3 decimal
              digits),
           </li>
      
           <li>
              
              <code>{0:P-,0}</code>                50.0
              -&gt; &quot;5000 %&quot;            (omits group separators and removes decimal
              digits)
           </li>
      
           <li>
              
              <code>{0:P-0}</code>                   0.50
              -&gt; &quot;50 %&quot;                (removes decimal part when zero; includes
              percent symbol and group separators)<br />
              <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 0.501
                 -&gt; &quot;50.1 %&quot;
              </p>
      
           </li>
      
      
           <li>
              
              <code>{0:P-%-,-0}</code>         0.50
              -&gt; &quot;50&quot;                    (removes decimal part when zero; omits percent symbol and group separators)<br />
              <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 0.501
                 -&gt; &quot;50.1&quot;
              </p>
      
           </li>
        </ul>
      

    Add more by creating a class that implements the IAdvancedFormatterPlugIn interface.

    Getting started

    Either add the Visual Studio project into your application or add the source code files.

    Using the Visual Studio project file

    After adding the project, add a reference from your application to the project.

    Using the source code

    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.

    Using AdvancedFormatProvider

    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.

    Do not need to pass a 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."

    Need to pass a CultureInfo object

    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."

    Creating your own plug-in

    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.

About

IFormatProvider for .net's String.Format to improve formatting integer, currency, and percent values

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages