DUnitConversion is a simple library providing tools for unit conversion in runtime.
To use this package, run the following command in your project's root directory:
Put the following dependency into your project's dependences section:
Library for D programming language that provides tools for runtime unit conversion.
This is a port of my Qt-based library QUnitConversion which is available here
DUnitConversion stores units as strings grouped by "family" (for example length or temperature). Each family has its own base unit, conversion inside a family is performed by converting through base unit
providing conversion from any unit to any other unit in a family. Conversion rules can be added dynamically
and/or loaded from JSON-formatted string so you can add your own conversions if needed. An example of
an input JSON file is provided in
Note that each unit should have a unique name, as long as conversion is unit name-based.
DUnitConversion supports aliases for unit names, see aliases example below.
Documentation is available here
Author: Dmitriy Linev
- Load unit conversion rules from JSON files
- Support for unit aliases
- Convert values directly
- Bulk convert is supported with linear convert functions (see examples)
auto convertor = new UnitConvertor; // fill the convertor instance with rules convertor.addConversionRule(ConversionRule("length", "m", "km", LinearFunction(0.001, 0))); convertor.addConversionRule(ConversionRule("length", "m", "cm", LinearFunction(100, 0))); // you can convert a single value double km = convertor.convert(50, "km", "m"); // returns value of a 50 km converted to meters // or get a linear function that holds conversion from one unit to another // to apply this conversion to many numbers without finding a conversion each time LinearFunction convertFunction = convertor.convert("m", "km"); double  meters; // meters is filled here... double  kilometers; foreach (m; meters) kilometers ~= convertFunction.y(m);
auto convertor = new UnitConvertor; // load conversion rules from JSON convertor.loadFromJson(parseJSON(readText("conversion_rules.json"))); // load aliases for unit names from JSON convertor.loadAliasesFromJson(parseJSON(readText("aliases.json"))); double km; km = convertor.convert(50, "km", "m"); // returns value of a 50 km converted to meters km = convertor.convert(50, "km", "meter"); // "meter" is an alias for "m" written in loaded json km = convertor.convert(50, "km", "meters"); // and "meters" a as well
|Tests source code.|
dunitconversion is available in dub. If you're using dub run
dub add dunitconversion in your project folder and dub will add dependency and fetch the latest version.
- Registered by Dmitriy Linev
- 1.0.1 released 7 months ago
- Copyright © 2021, Linev