Repository navigation
Advanced Usage
This library is intentionally simple — a single struct with unit extension methods and standard operators. There isn't a large surface of advanced features, but a few things are worth calling out.
Because every Energy is stored internally as joules, you can freely combine creation methods across namespaces — electrical watt-hour units, chemistry-textbook calories, physics-textbook electronvolts:
using MarcusMedina.Units.Energy.Metric;
using MarcusMedina.Units.Energy.Scientific;
Energy combined = 1.Kilowatthours() + 500.Kilocalories();
double joules = combined.ToJoules(); // 3,600,000 + 2,092,000 = 5,692,000Dividing one Energy by another returns a plain double ratio (not an Energy), useful for "how many times more energy" comparisons:
using MarcusMedina.Units.Energy.Scientific;
Energy tnt = 1.TonsOfTNT();
Energy ev = 1.Electronvolts();
double ratio = tnt / ev; // ≈ 2.61e28Dividing an Energy by a double instead scales it down and still returns an Energy:
Energy half = tnt / 2.0;Energy implements IComparable<Energy> and all six comparison operators, so values from unrelated unit families compare correctly without manual conversion:
using MarcusMedina.Units.Energy.US;
using MarcusMedina.Units.Energy.Metric;
bool bigger = 1.BTUs() > 1.Kilojoules(); // true — 1 BTU ≈ 1055 J, more than 1 kJ (1000 J)Energy.ToString() prints the joule value using invariant culture, formatted as "{value} J":
Console.WriteLine(1.Kilojoules()); // "1000 J"There is no built-in support for formatting in a different unit — call the relevant To...() method and format the double yourself if you need e.g. "1 kcal".