Class PrefixUnitFormatter
Defines the string formatting of objects having an implementation of IPrefixUnit.
Examples
Format prefix units with custom numeric and culture-specific representations. This example demonstrates creating a prefix unit with custom format options, shows the default string representation, uses a PrefixUnitFormatter instance for custom formatting with a CultureInfo, and displays how to create a metric prefix table from a prefix unit to show multiple scaled representations:
using System;
using System.Globalization;
using Codebelt.Unitify;
namespace Unitify.Samples;
public class PrefixUnitFormatterExample
{
public static void Main()
{
// Create a formatted prefix unit
var kilowatt = new PrefixUnit(Unit.Watt, 5.5, DecimalPrefix.Kilo, options =>
{
options.NumberFormat = "F2"; // 2 decimal places
});
// Default formatting
Console.WriteLine($"Default: {kilowatt}");
// Create a formatter instance
var formatter = new PrefixUnitFormatter();
// Format with custom format string
var formatted = formatter.Format("{0:F1} {1}", kilowatt, CultureInfo.CurrentCulture);
Console.WriteLine($"Formatted: {formatted}");
// Format with metric prefix table for detailed output
var table = new MetricPrefixTable(kilowatt);
Console.WriteLine("Prefix table:");
Console.WriteLine(table.ToString());
}
}
public class PrefixUnitFormatter : IFormatProvider, ICustomFormatter
- Inheritance
-
PrefixUnitFormatter
- Implements
Methods
Format(string, object, IFormatProvider)
Converts the value of a specified arg to an equivalent string representation using specified format and culture-specific format formatProvider.
public string Format(string format, object arg, IFormatProvider formatProvider)
Parameters
formatstringA format string containing formatting specifications.
argobjectAn object that implements the IPrefixUnit interface.
formatProviderIFormatProviderAn object that supplies format information about
arg.
Returns
- string
The string representation of the value of
arg, formatted as specified byformatandformatProvider.
GetFormat(Type)
Returns an object that provides formatting services for the specified type.
public object GetFormat(Type formatType)
Parameters
formatTypeTypeAn object that specifies the type of format object to return.
Returns
- object
An instance of the object specified by
formatType, if the IFormatProvider implementation can supply that type of object; otherwise, null.