Codebelt

Unitify API by Codebelt

Work with unit values through prefix-aware models instead of ad hoc conversion code.

1 NuGet package .NET 10.0 / .NET 9.0 MIT v10.0.8

Overview

Unitify gives .NET code a structured way to represent measured values instead of treating prefixes, symbols, and conversions as unrelated string or math concerns. The repository focuses on keeping one base value authoritative while still letting callers project that value into decimal or binary forms that match the domain they are working in.

That matters in places where the same quantity has multiple valid presentations, such as bytes versus kibibytes or grams versus kilograms. The library turns those projections into explicit APIs rather than leaving them scattered across formatting helpers and hand-written multipliers.

Concepts

The concepts in this repository all revolve around one idea: a measured quantity should preserve its base meaning even when you render it with a different prefix, symbol, or naming style.

Base units stay authoritative

The core model in Codebelt.Unitify separates a base unit definition from the prefixed value you display to users. BaseUnit, Unit, and PrefixUnit keep category, name, symbol, and value together so conversions do not lose the original unit identity while PrefixUnitExtensions.ToBaseValue() and ToBaseUnit() make that round-trip explicit.

Prefixes are first-class, not formatting tricks

The repository treats prefixes as data with their own base, exponent, multiplier, and conversion rules. In Codebelt.Unitify, DecimalPrefix and BinaryPrefix expose complete supported prefix sets, and Prefix plus PrefixExtensions let callers move between a base unit and a prefixed representation without reimplementing the exponent math in consumer code.

Tables expose alternate projections of the same value

The most distinctive workflow is to start from one IPrefixUnit, then expand it into a table of related projections. Codebelt.Unitify uses MetricPrefixTable for SI-style scaling and DataPrefixTable for byte, bit, and transfer-rate scenarios, with PrefixTableExtensions providing direct lookups such as GibiOrDefault() or MegaOrDefault() when callers need one specific view instead of the whole sequence.

Formatting is part of the model boundary

Formatting is not bolted on after conversion. In Codebelt.Unitify, UnitFormatOptions, UnitFormatter, and PrefixUnitFormatter make naming style, number format, and symbol selection part of the unit pipeline, which is why the same value can be rendered as GB, GiB, gigabyte, or gibibyte without changing the stored base quantity.

Usage guidance

Use this library when your code needs to preserve unit semantics while moving between multiple prefix representations, especially for storage, transfer-rate, or SI-style numeric output. It fits best when the same value is displayed, compared, or inspected in more than one scale and those projections need to stay consistent.

Avoid it when a plain number and one fixed label are enough. If the code never needs prefix-aware conversion, table inspection, or formatting control, direct numeric operations are simpler than introducing unit and prefix abstractions.

Family packages