Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .docfx/Dockerfile.docfx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG NGINX_VERSION=1.31.0-alpine
ARG NGINX_VERSION=1.31.2-alpine

FROM --platform=$BUILDPLATFORM nginx:${NGINX_VERSION} AS base
RUN rm -rf /usr/share/nginx/html/*
Expand Down
38 changes: 35 additions & 3 deletions .docfx/api/namespaces/Codebelt.Unitify.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,46 @@
uid: Codebelt.Unitify
summary: *content
---
The `Codebelt.Unitify` namespace offers types that simplify unit management with comprehensive metric and binary support for prefixes, multiples, and submultiples.

Simplify unit measurement and conversion with **Codebelt.Unitify**, a comprehensive .NET library for managing units with SI unit support, metric prefixes (kilo, mega, milli, micro, etc.), and binary prefixes (kibi, mebi, gibi, etc.).

To get started, use [`UnitFactory`](xref:Codebelt.Unitify.UnitFactory) to create SI units, or access predefined SI base units via the [`Unit`](xref:Codebelt.Unitify.Unit) class.

## Start Here

Begin with **`UnitFactory`** — the primary API for creating SI units with custom precision and base values. Call static methods like `UnitFactory.CreateMeter()` or `UnitFactory.CreateWatt()` to construct units programmatically.

If you only need standard predefined base units, use the **`Unit`** class directly instead (e.g., `Unit.Meter`, `Unit.Kilogram`) to avoid factory overhead.

## When to Use

Use Codebelt.Unitify when you need to:

- Work with SI base units (meter, kilogram, second, ampere, kelvin, mole, candela) and derived units
- Apply metric (decimal) or binary prefixes to create unit variations (e.g., kilometer, megabyte, gibibyte)
- Convert between different prefix scales while preserving semantic meaning
- Format units using metric, data-centric, or custom naming conventions

## Getting Started

**Start here:** Call `UnitFactory.CreateMeter()`, `UnitFactory.CreateWatt()`, or other static factory methods to construct SI units with your desired precision and base values. `UnitFactory` is the primary entry point for programmatic unit creation.

**Alternative:** If you need one of the standard predefined SI base units (meter, kilogram, second, ampere, kelvin, mole, candela), access them directly via static properties on the `Unit` class (e.g., `Unit.Meter`, `Unit.Kilogram`) to avoid factory overhead.

Once you have a unit, you can apply metric or binary prefixes in three ways:

- **Single prefix**: Use `PrefixUnit` to combine a specific prefix with a unit (e.g., create a kilometer from `Unit.Meter` and `DecimalPrefix.Kilo`).
- **Full metric scale table**: Create a `MetricPrefixTable` to explore all available decimal-prefix representations (kilo, mega, giga, etc.) at once.
- **Full binary scale table**: Create a `DataPrefixTable` to explore all available binary-prefix representations (kibi, mebi, gibi, etc.) at once for data/storage contexts.

Choose `MetricPrefixTable` for general scientific and engineering units; choose `DataPrefixTable` exclusively for data storage and network bandwidth to avoid mixing decimal (1 kB = 1000 bytes) and binary (1 KiB = 1024 bytes) scales.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

### Extension Methods
## Extension Members

|Type|Ext|Methods|
|--:|:-:|---|
|Prefix|⬇️|`ToPrefixUnit`, `ToBaseUnit`|
|PrefixTable|⬇️|`QuectoOrDefault`, `RontoOrDefault`, `YoctoOrDefault`, `ZeptoOrDefault`, `AttoOrDefault`, `FemtoOrDefault`, `PicoOrDefault`, `NanoOrDefault`, `MicroOrDefault`, `MilliOrDefault`, `CentiOrDefault`, `DeciOrDefault`, `DecaOrDefault`, `HectoOrDefault`, `KiloOrDefault`, `MegaOrDefault`, `GigaOrDefault`, `TeraOrDefault`, `PetaOrDefault`, `ExaOrDefault`, `ZettaOrDefault`, `YottaOrDefault`, `RonnaOrDefault`, `QuettaOrDefault`, `KibiOrDefault`, `MebiOrDefault`, `GibiOrDefault`, `TebiOrDefault`, `PebiOrDefault`, `ExbiOrDefault`, `ZebiOrDefault`, `YobiOrDefault`|
|PrefixTable|⬇️|`QuectoOrDefault`, `RontoOrDefault`, `YoctoOrDefault`, `ZeptoOrDefault`, `AttoOrDefault`, `FemtoOrDefault`, `PicoOrDefault`, `NanoOrDefault`, `MicroOrDefault`, `MilliOrDefault`, `CentiOrDefault`, `DeciOrDefault`, `DecaOrDefault`, `HectoOrDefault`, `KiloOrDefault`, `MegaOrDefault`, `GigaOrDefault`, `TeraOrDefault`, `PetaOrDefault`, `ExaOrDefault`, `ZettaOrDefault`, `YottaOrDefault`, `RonnaOrDefault`, `QuettaOrDefault`, `QuebiOrDefault`, `RobiOrDefault`, `KibiOrDefault`, `MebiOrDefault`, `GibiOrDefault`, `TebiOrDefault`, `PebiOrDefault`, `ExbiOrDefault`, `ZebiOrDefault`, `YobiOrDefault`|
|PrefixUnit|⬇️|`ToPrefixValue`, `ToBaseValue`, `ToBaseUnit`, `ToPrefixString`, `ToMetricPrefixTable`, `ToDataPrefixTable`|
34 changes: 34 additions & 0 deletions .docfx/api/types/Codebelt.Unitify.BaseUnit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
uid: Codebelt.Unitify.BaseUnit
---

## Examples

Represent a unit of measurement with its category, name, and symbol. This example creates several `BaseUnit` instances, demonstrates property access, and shows how to compare units for equality to verify that units with identical properties are considered equal:

```csharp
using System;
using Codebelt.Unitify;

namespace Unitify.Samples;

public class BaseUnitExample
{
public static void Main()
{
// Create a meter base unit
var meter = new BaseUnit("Length", "Meter", "m");

Console.WriteLine($"Category: {meter.Category}");
Console.WriteLine($"Name: {meter.Name}");
Console.WriteLine($"Symbol: {meter.Symbol}");

// Create a kilogram base unit
var kilogram = new BaseUnit("Mass", "Kilogram", "kg");

// Compare base units
var sameUnit = new BaseUnit("Length", "Meter", "m");
Console.WriteLine($"meter == sameUnit: {meter == sameUnit}");
}
}
```
34 changes: 34 additions & 0 deletions .docfx/api/types/Codebelt.Unitify.BinaryPrefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
uid: Codebelt.Unitify.BinaryPrefix
---

## Examples

Work with binary prefixes (kibi, mebi, gibi, etc.) for data storage measurements. This example retrieves binary prefix constants, demonstrates property access to find their symbols and multiplier values, converts raw byte values to binary scales using `ToPrefixValue()`, and shows how to construct `PrefixUnit` objects that combine a unit with a binary prefix:

```csharp
using System;
using Codebelt.Unitify;

namespace Unitify.Samples;

public class BinaryPrefixExample
{
public static void Main()
{
// Get the kibi prefix (2^10)
var kibi = BinaryPrefix.Kibi;
Console.WriteLine($"Kibi symbol: {kibi.Symbol}");
Console.WriteLine($"Kibi multiplier: {kibi.Multiplier}");

// Work with data prefix values
var bytes = 1024.0;
var kibibytes = kibi.ToPrefixValue(bytes); // Convert to kibi scale
Console.WriteLine($"{bytes} bytes = {kibibytes} KiB");

// Create a base unit value with binary prefix
var dataUnit = new PrefixUnit(Unit.Byte, 1048576, BinaryPrefix.Mebi); // 1 MiB
Console.WriteLine($"Data unit: {dataUnit}");
}
}
```
34 changes: 34 additions & 0 deletions .docfx/api/types/Codebelt.Unitify.DataPrefixTable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
uid: Codebelt.Unitify.DataPrefixTable
---

## Examples

Browse all binary prefix representations of a data storage unit. This example creates a `DataPrefixTable` from a byte quantity, then displays both the full table of all binary-scaled representations and an aggregate summary showing which scales are available for that unit value:

```csharp
using System;
using Codebelt.Unitify;

namespace Unitify.Samples;

public class DataPrefixTableExample
{
public static void Main()
{
// Create a unit representing 1,048,576 bytes (1 MiB)
var byteUnit = UnitFactory.CreateByte(1048576);

// Create a data prefix table to see all binary scales
var table = new DataPrefixTable(byteUnit);

// Display all representations
Console.WriteLine("Data storage representations:");
Console.WriteLine(table.ToString());

// Show aggregate summary
Console.WriteLine("\nAggregate:");
Console.WriteLine(table.ToAggregateString());
}
}
```
40 changes: 40 additions & 0 deletions .docfx/api/types/Codebelt.Unitify.DecimalPrefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
uid: Codebelt.Unitify.DecimalPrefix
---

## Examples

Use decimal (metric) prefixes (kilo, mega, milli, micro, etc.) for SI measurements. This example retrieves prefix constants, extracts their symbols and multiplier values, converts raw measurements to prefixed scales using `ToPrefixValue()`, and shows how to construct `PrefixUnit` objects that apply different scales to physical quantities:

```csharp
using System;
using Codebelt.Unitify;

namespace Unitify.Samples;

public class DecimalPrefixExample
{
public static void Main()
{
// Get the kilo prefix (10^3)
var kilo = DecimalPrefix.Kilo;
Console.WriteLine($"Kilo symbol: {kilo.Symbol}");
Console.WriteLine($"Kilo multiplier: {kilo.Multiplier}");

// Convert a value to kilo scale
var meters = 5000.0;
var kilometers = kilo.ToPrefixValue(meters);
Console.WriteLine($"{meters} m = {kilometers} km");

// Create a watt unit with kilo prefix
var kilowatt = new PrefixUnit(Unit.Watt, 5, DecimalPrefix.Kilo);
Console.WriteLine($"Power: {kilowatt}");

// Work with micro prefix for small values
var micro = DecimalPrefix.Micro;
var amperes = 0.000005;
var microamperes = micro.ToPrefixValue(amperes);
Console.WriteLine($"{amperes} A = {microamperes} µA");
}
}
```
34 changes: 34 additions & 0 deletions .docfx/api/types/Codebelt.Unitify.MetricPrefixTable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
uid: Codebelt.Unitify.MetricPrefixTable
---

## Examples

Browse all decimal prefix representations of a SI unit. This example creates a `MetricPrefixTable` from a watt quantity, then displays both the full table showing all decimal-scaled (power-of-10) representations and an aggregate summary indicating which SI prefix scales are available for that unit value:

```csharp
using System;
using Codebelt.Unitify;

namespace Unitify.Samples;

public class MetricPrefixTableExample
{
public static void Main()
{
// Create a unit: 11,745 watts (approximately 11.745 kW)
var wattUnit = UnitFactory.CreateWatt(11745);

// Create a metric prefix table to see all decimal scales
var table = new MetricPrefixTable(wattUnit);

// Display all representations from quecto to yotta
Console.WriteLine("Metric prefix representations:");
Console.WriteLine(table.ToString());

// Show aggregate summary
Console.WriteLine("\nAggregate:");
Console.WriteLine(table.ToAggregateString());
}
}
```
31 changes: 31 additions & 0 deletions .docfx/api/types/Codebelt.Unitify.NamingStyle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
uid: Codebelt.Unitify.NamingStyle
---

## Examples

Control how units are formatted as text by choosing between symbol-based and compound naming styles. This example creates a prefix unit, displays its default representation, then creates another using a setup action to apply `NamingStyle.Compound` so the output shows full names instead of abbreviations:

```csharp
using System;
using Codebelt.Unitify;

namespace Unitify.Samples;

public class NamingStyleExample
{
public static void Main()
{
// Create a kilometer with Default naming
var kilometer = new PrefixUnit(Unit.Meter, 1.0, DecimalPrefix.Kilo);
Console.WriteLine($"Default: {kilometer}");

// Use Compound naming style to show compound names
var compoundKilometer = new PrefixUnit(Unit.Meter, 5.0, DecimalPrefix.Kilo, o =>
{
o.Style = NamingStyle.Compound;
});
Console.WriteLine($"Compound: {compoundKilometer}");
}
}
```
54 changes: 54 additions & 0 deletions .docfx/api/types/Codebelt.Unitify.PrefixExtensions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
uid: Codebelt.Unitify.PrefixExtensions
---

## Examples

Use extension methods on prefix types to extract base unit information and convert values. This example demonstrates a realistic workflow where you start with decimal and binary prefix instances (prerequisites: `DecimalPrefix.Kilo` and `BinaryPrefix.Kibi`), then use the `ToPrefixUnit()` extension method to combine a prefix with a unit type to create scaled units (setup), and finally invoke `ToBaseUnit()` and `ToPrefixValue()` methods to demonstrate conversion and scaling operations. The outcome shows how extension methods simplify the creation of prefix-unit combinations and value scaling without requiring manual unit construction:

```csharp
using System;
using Codebelt.Unitify;

namespace Unitify.Samples;

public class PrefixExtensionsExample
{
public static void Main()
{
// Create instances of DecimalPrefix and BinaryPrefix
var kilo = DecimalPrefix.Kilo;
var kibi = BinaryPrefix.Kibi;

// Use the ToPrefixUnit extension method to create scaled units
// Create a kilo-meter by combining the Kilo prefix with a meter unit
var meterUnit = new UnitTestWrapper(); // Implement IUnit
var kilometer = kilo.ToPrefixUnit(meterUnit);
Console.WriteLine($"Kilometer: {kilometer}");

// Create a kibi-byte using the Kibi prefix
var byteUnit = new UnitTestWrapper(); // Implement IUnit
var kibibyte = kibi.ToPrefixUnit(byteUnit);
Console.WriteLine($"Kibibyte: {kibibyte}");

// Use ToBaseUnit extension method to create a unit with prefix applied to a base unit
var wattUnit = kilo.ToBaseUnit(Unit.Watt, 5.0);
Console.WriteLine($"Kilowatt (5 kW): {wattUnit}");

// Use ToPrefixValue to convert a raw value to prefix scale
var rawValue = 5000.0;
var prefixedValue = kilo.ToPrefixValue(rawValue);
Console.WriteLine($"{rawValue} base units = {prefixedValue} kilo units");
}
}

// Simple test implementation of IUnit
public class UnitTestWrapper : IUnit
{
public string Category => "Data";
public string Name => "byte";
public string Symbol => "B";
public double Value => 1.0;
public UnitFormatOptions FormatOptions => new();
}
```
31 changes: 31 additions & 0 deletions .docfx/api/types/Codebelt.Unitify.PrefixStyle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
uid: Codebelt.Unitify.PrefixStyle
---

## Examples

Control how data prefix tables format output using binary (powers of 1024) or decimal (powers of 10) scales. This example demonstrates the `ToString(PrefixStyle)` method on `DataPrefixTable` to display the same byte quantity in two different prefix styles, allowing you to see how the same value appears with binary vs. decimal formatting:

```csharp
using System;
using Codebelt.Unitify;

namespace Unitify.Samples;

public class PrefixStyleExample
{
public static void Main()
{
// Create a data unit table
var byteUnit = UnitFactory.CreateByte(1048576); // 1,048,576 bytes
var dataTable = new DataPrefixTable(byteUnit);

// Display with binary style formatting (1024-based prefixes)
Console.WriteLine("Binary prefix style (powers of 1024):");
Console.WriteLine(dataTable.ToString(PrefixStyle.Binary));

Console.WriteLine("\nDecimal prefix style (powers of 10):");
Console.WriteLine(dataTable.ToString(PrefixStyle.Decimal));
}
}
```
Loading
Loading