Format a value the same way String.Format does.
The Default formatter is the fallback formatter. It converts a value to text using the standard .NET formatting logic. Apply it explicitly with the name d, but in most cases it applies implicitly when no other formatter handles the value. Numeric and date format strings such as C, N2, and d work exactly as they do with String.Format.
Cultural formatting, such as currency symbols and date order, follows the active culture. Pass a CultureInfo (or any IFormatProvider) to Smart.Format to control it; otherwise the current culture is used.
var amount = 1234567.89;
// "The cost is $1,234,567.89"
Smart.Format(new CultureInfo("en-US"), "The cost is {0:C}", amount);
// "The cost is 1 234 567,89 €"
Smart.Format(new CultureInfo("fr-FR"), "The cost is {0:C}", amount);
| Smart String | Argument | Result |
|---|---|---|
The cost is {0:C} |
1234567.89 (US culture) |
The cost is $1,234,567.89 |
The cost is {0:C} |
1234567.89 (French culture) |
The cost is 1 234 567,89 € |
Today is {0:d} |
a DateTime (US culture) |
Today is 11/8/2021 |
Today is {0:d} |
a DateTime (French culture) |
Today is 08/11/2021 |
The formatter applies the value’s IFormattable.ToString implementation when the format provider supplies one, then falls back to the value’s ToString.
A custom ICustomFormatter from the format provider takes precedence, so you can plug in your own formatting.
When the format contains nested placeholders, those are formatted first instead of the value being formatted directly.