Codebelt
v10.0.10

Codebelt.Unitify

Model SI and data units with consistent prefix math and readable output.

.NET 10.0 / .NET 9.0 MIT 4,025 downloads

Overview

Codebelt.Unitify models a numeric value together with its base unit and optional prefix, then gives you consistent ways to project that value into decimal or binary representations. It covers common data units such as bits, bytes, and bit-per-second alongside a broader SI-oriented catalog that can be created through a single factory API.

The package is strongest when the same quantity needs more than one valid representation, such as decimal versus binary storage output or a metric value rendered at a more suitable scale. Instead of hand-rolling multiplier math and display strings, you work with unit, prefix, and table abstractions that keep the base value and formatted output aligned.

Key APIs

UnitFactory is the main entry point for consumers. It creates concrete IPrefixUnit instances for built-in units such as CreateByte, CreateBitPerSecond, CreateWatt, and CreateKilogram, and CreateUnit lets you register a custom category, name, and symbol when the built-in catalog is not enough.

DecimalPrefix defines the SI prefix set from Quecto through Quetta and exposes the full sequence through MetricPrefixes. Use it when you need an explicit metric prefix such as DecimalPrefix.Kilo or when you want to enumerate the supported decimal multiples and submultiples.

BinaryPrefix defines IEC binary prefixes from Kibi through Quebi, along with ConvertBitsToBytes, ConvertBytesToBits, and the BinaryPrefixes sequence. That makes it the package's binary counterpart to DecimalPrefix for data-size and transfer-rate work.

MetricPrefixTable expands any IUnit into its decimal prefix projections. Its enumerator, ToString(), and ToAggregateString(...) methods are what make it practical to move from a raw base value to the first sensible prefix or a full inspection view of every supported SI scale.

DataPrefixTable specializes PrefixTable for Unit.Bit, Unit.Byte, and Unit.BitPerSecond. Its factory methods such as CreateByteTableFromBytes(...) and CreateBitTableFromBits(...) normalize the base value first, then let you inspect both decimal and binary interpretations of the same data quantity.

PrefixUnitExtensions connects a prefixed unit back to the rest of the model. ToBaseValue(), ToBaseUnit(), ToMetricPrefixTable(), ToDataPrefixTable(), and ToPrefixString() let you switch between stored values, formatted strings, and table views without rebuilding the unit by hand.

PrefixTableExtensions gives named lookups over computed tables such as GibiOrDefault(), MegaOrDefault(), and MicroOrDefault(). These helpers matter when a caller needs one specific projection from a table instead of iterating the whole sequence.

Basic usage

using Codebelt.Extensions.Xunit;
using Codebelt.Unitify;
using Xunit;

namespace MyProject.Tests;

public sealed class FileSizePrefixTableTest : Test
{
    public FileSizePrefixTableTest(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void ShouldProjectFileSizeIntoDecimalAndBinaryPrefixes()
    {
        var fileSize = UnitFactory.CreateByte(3812840313);
        var prefixes = fileSize.ToDataPrefixTable();

        TestOutput.WriteLine(prefixes.ToAggregateString());
        TestOutput.WriteLine(prefixes.ToString(PrefixStyle.Decimal));

        Assert.Equal("3.81284 GB", prefixes.ToString(PrefixStyle.Decimal));
        Assert.Equal("3.550984 GiB", prefixes.GibiOrDefault()!.ToString());
    }
}

Use this pattern when a byte value needs both decimal and binary projections without duplicating conversion logic in the calling code. It matters because DataPrefixTable keeps the underlying base value intact while the table and lookup extensions expose the representation that fits the current output surface.

Installation

dotnet add package Codebelt.Unitify

Usage guidance

Adopt this package when your code needs a stable unit vocabulary, prefix-aware formatting, or repeatable projection of the same value across decimal and binary scales. If you only need a one-off formatted number or a raw numeric value, simpler numeric code is usually the better fit because Codebelt.Unitify is built around modeling a base unit and its prefix relationships.