Table of Contents

Class UnitFormatOptions

Namespace
Codebelt.Unitify
Assembly
Codebelt.Unitify.dll

Configuration options for implementations of IBaseUnit.

Examples

Configure how units are formatted when converted to strings by creating and using UnitFormatOptions instances. This example demonstrates creating UnitFormatOptions directly with custom settings, applying those settings to units via factory methods, and shows how different number formats and naming styles produce different string representations:

using System;
using Codebelt.Unitify;

namespace Unitify.Samples;

public class UnitFormatOptionsExample
{
    public static void Main()
    {
        // Create a UnitFormatOptions instance directly
        var options = new UnitFormatOptions
        {
            NumberFormat = "F2",
            Style = NamingStyle.Compound
        };
        
        // Apply the options to a unit via UnitFactory
        var watt = UnitFactory.CreateWatt(1234.567, setup: opts =>
        {
            opts.NumberFormat = options.NumberFormat;
            opts.Style = options.Style;
        });
        
        Console.WriteLine($"With custom options: {watt}");
        
        // Create another UnitFormatOptions with different settings
        var symbolOptions = new UnitFormatOptions
        {
            NumberFormat = "#,##0.##",
            Style = NamingStyle.Symbol
        };
        
        // Display the configured options
        Console.WriteLine($"Number Format: {symbolOptions.NumberFormat}");
        Console.WriteLine($"Style: {symbolOptions.Style}");
        
        // Apply symbol options to a meter unit
        var meter = UnitFactory.CreateMeter(1500.0, setup: opts =>
        {
            opts.NumberFormat = symbolOptions.NumberFormat;
            opts.Style = symbolOptions.Style;
        });
        
        Console.WriteLine($"Meter with symbol options: {meter}");
    }
}
public class UnitFormatOptions : FormattingOptions, IValidatableParameterObject, IParameterObject
Inheritance
UnitFormatOptions
Implements
Inherited Members

Constructors

UnitFormatOptions()

Initializes a new instance of the UnitFormatOptions class.

public UnitFormatOptions()

Remarks

The following table shows the initial property values for an instance of UnitFormatOptions.

PropertyInitial Value
StyleSymbol
NumberFormat#,##0.######
FormatProviderInvariantCulture

Properties

NumberFormat

Gets or sets the desired number format.

public string NumberFormat { get; set; }

Property Value

string

The desired number format.

Style

Gets or sets the desired naming style.

public NamingStyle Style { get; set; }

Property Value

NamingStyle

The desired naming style.

See Also