-
-
Notifications
You must be signed in to change notification settings - Fork 105
Default _ DefaultFormatter
axunonb edited this page Mar 30, 2022
·
8 revisions
The DefaultFormatter
takes care of the default formatting, pretty much with the same logic as string.Format
.
In particular, it is required for
- output of nested
Placedholder
s (going beyondstring.Format
capabilities) - output, when
IFormatProvider
withICustomFormatter
is in place as an argument toSmartFormatter.Format(...)
- output of any arguments implementing
IFormattable
- output of
string
variables - output of any object (in which case its
.ToString()
method is invoked)
{ Any Value : d : format }
Value | formatter name | format |
---|---|---|
Any value | "d" or implicit | optional: format to use |
string Name
: default isd
The name to use a named formatter
// implicit invokation
Smart.Format("{0}", 1234);
// explicit invokation by formatter name
Smart.Format("{0:d}", 1234);
// with CultureInfo
Smart.Format(CultureInfo.GetCultureInfo("en-US"), "{0:d}", 1234);
// all output: "1234"
// A simple class
public class FmtDemo : IFormattable
{
public string ToString(string? format, IFormatProvider? p)
{
return $"{format} implmenting IFormattable";
}
}
Smart.Format("{0:FmtDemo}", new FmtDemo());
// Outputs: "FmtDemo implementing IFormattable"
var amount = 123.456M;
var c = new System.Globalization.CultureInfo("fr-FR");
// Only works for indexed placeholders
var formatDelegate = new SmartFormat.Utilities.FormatDelegate((format, culture) => $"{format}: {amount.ToString(c)}");
Smart.Format("{0:The amount is}", formatDelegate);
// Outputs: "The amount is: 123,456"
public class ReverseFormatProvider : IFormatProvider
{
public object GetFormat(Type? formatType)
{
if (formatType == typeof(ICustomFormatter)) return new ReverseFormatAndArgumentFormatter();
return new object();
}
}
public class ReverseFormatAndArgumentFormatter : ICustomFormatter
{
public string Format(string? format, object? arg, IFormatProvider? formatProvider)
{
return new string(format!.Reverse().Select(c => c).ToArray()) + ": " +
new string((arg as string ?? "?").Reverse().Select(c => c).ToArray());
}
}
Use the custom IFormatProvider
:
// only these 2 extensions are needed
var smart = new SmartFormatter()
.AddExtensions(new DefaultFormatter())
.AddExtensions(new DefaultSource());
smart.Format(new ReverseFormatProvider(), "0:format", "value");
// Outputs: "tamrof: eulav"
// (value and format arguments reversed)
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License