Units.NET was not designed for high-precision, but rather a tool of convenience and simplicity. As a result, there is usually a small error involved in both representing a value of a unit and converting between units. We are open to ideas how to improve this, while still keeping it simple and convenient.
- A base unit is chosen for all quantities
- SI base unit is preferred where available, such as
LengthUnit.MeterandVolumeUnit.CubicMeter. - See the
BaseUnitschema reference for how it is declared. MassUnit.Gramwas chosen to better support SI prefixes likekilo,megaetc.
- SI base unit is preferred where available, such as
- The value is typically represented by a
doublevalue (64-bit) - Conversions go via the base unit.
- Centimeter => Meter => Kilometer
- As a result, most conversions have a rounding error. The error is larger for units that are way larger or way smaller than the base unit.
- A rounding error of
1e-5is accepted for round-trip conversion of most units in the library. In many use cases this is sufficient, but for others this may not be acceptable. - In v6, unit conversion definitions can be customized before the converter is built. This can be used to override a built-in conversion factor or add conversion functions for custom quantities.
Built-in unit definitions can be customized through UnitsNetSetup.ConfigureDefaults() before the default setup is used:
UnitsNetSetup.ConfigureDefaults(builder => builder.ConfigureQuantity(() =>
Pressure.PressureInfo.CreateDefault(units =>
units.Configure(PressureUnit.InchOfWaterColumn, unit =>
unit.WithConversionFactorFromBase(999)))));
var pressure = Pressure.FromPascals(1);
double value = pressure.As(PressureUnit.InchOfWaterColumn); // 999For isolated conversions, create a custom QuantityInfo and pass it to a custom UnitConverter instead of changing the global defaults. See Samples/UnitsNetSetup.Configuration/ConfigureWithCustomConversions.cs for a complete example.
When adding test values for unit conversions:
- Use at least 7 significant figures where possible
- Beyond 16 significant digits is not useful due to
doubleprecision limits - Tests accept an error margin of
1e-5for most units