Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Working with Units and Amounts

, 17 Jul 2013 CPOL
The ultimate Unit and Amount classes for your business and physics applications!
using System;
using TypedUnits;

namespace StandardUnits
{
    public static class SIUnitTypes
    {
        public static readonly UnitType Length = new UnitType("metre");
        public static readonly UnitType Mass = new UnitType("kilogram");
        public static readonly UnitType Time = new UnitType("second");
        public static readonly UnitType ElectricCurrent = new UnitType("ampere");
        public static readonly UnitType ThermodynamicTemperature = new UnitType("kelvin");
        public static readonly UnitType AmountOfSubstance = new UnitType("mole");
        public static readonly UnitType LuminousIntensity = new UnitType("candela");
    }

    [UnitDefinitionClass]
    public static class LengthUnits
    {
        public static readonly Unit Meter = new Unit("meter", "m", SIUnitTypes.Length);
        public static readonly Unit MilliMeter = new Unit("millimeter", "mm", 0.001 * Meter);
        public static readonly Unit CentiMeter = new Unit("centimeter", "cm", 0.01 * Meter);
        public static readonly Unit DeciMeter = new Unit("decimeter", "dm", 0.1 * Meter);
        public static readonly Unit DecaMeter = new Unit("decameter", "Dm", 10.0 * Meter);
        public static readonly Unit HectoMeter = new Unit("hectometer", "Hm", 100.0 * Meter);
        public static readonly Unit KiloMeter = new Unit("kilometer", "km", 1000.0 * Meter);

        public static readonly Unit Inch = new Unit("inch", "in", 0.0254 * Meter);
        public static readonly Unit Foot = new Unit("foot", "ft", 12.0 * Inch);
        public static readonly Unit Yard = new Unit("yard", "yd", 36.0 * Inch);
        public static readonly Unit Mile = new Unit("mile", "mi", 5280.0 * Foot);
        public static readonly Unit NauticalMile = new Unit("nautical mile", "nmi", 1852.0 * Meter);

        public static readonly Unit LightYear = new Unit("light-year", "ly", 9460730472580800.0 * Meter);
    }

    [UnitDefinitionClass]
    public static class SurfaceUnits
    {
        public static readonly Unit Meter2 = new Unit("meter²", "m²", LengthUnits.Meter.Power(2));
        public static readonly Unit Are = new Unit("are", "are", 100.0 * Meter2);
        public static readonly Unit HectAre = new Unit("hectare", "ha", 10000.0 * Meter2);
        public static readonly Unit KiloMeter2 = new Unit("kilometer²", "Km²", LengthUnits.KiloMeter.Power(2));
    }

    [UnitDefinitionClass]
    public static class VolumeUnits
    {
        public static readonly Unit Liter = new Unit("liter", "L", LengthUnits.DeciMeter.Power(3));
        public static readonly Unit MilliLiter = new Unit("milliliter", "mL", 0.001 * Liter);
        public static readonly Unit CentiLiter = new Unit("centiliter", "cL", 0.01 * Liter);
        public static readonly Unit DeciLiter = new Unit("deciliter", "dL", 0.1 * Liter);
        public static readonly Unit Meter3 = new Unit("meter³", "m³", LengthUnits.Meter.Power(3));
    }

    [UnitDefinitionClass]
    public static class TimeUnits
    {
        public static readonly Unit Second = new Unit("second", "s", SIUnitTypes.Time);
        public static readonly Unit MicroSecond = new Unit("microsecond", "μs", 0.000001 * Second);
        public static readonly Unit MilliSecond = new Unit("millisecond", "ms", 0.001 * Second);
        public static readonly Unit Minute = new Unit("minute", "min", 60.0 * Second);
        public static readonly Unit Hour = new Unit("hour", "h", 3600.0 * Second);
        public static readonly Unit Day = new Unit("day", "d", 24.0 * Hour);
    }

    [UnitDefinitionClass]
    public static class SpeedUnits
    {
        public static readonly Unit MeterPerSecond = new Unit("meter/second", "m/s", LengthUnits.Meter / TimeUnits.Second);
        public static readonly Unit KilometerPerHour = new Unit("kilometer/hour", "km/h", LengthUnits.KiloMeter / TimeUnits.Hour);
        public static readonly Unit MilePerHour = new Unit("mile/hour", "mi/h", LengthUnits.Mile / TimeUnits.Hour);
        public static readonly Unit Knot = new Unit("knot", "kn", 1.852 * SpeedUnits.KilometerPerHour);
    }

    [UnitDefinitionClass]
    public static class MassUnits
    {
        public static readonly Unit KiloGram = new Unit("kilogram", "Kg", SIUnitTypes.Mass);
        public static readonly Unit Gram = new Unit("gram", "g", 0.001 * KiloGram);
        public static readonly Unit MilliGram = new Unit("milligram", "mg", 0.001 * Gram);
        public static readonly Unit Ton = new Unit("ton", "ton", 1000.0 * KiloGram);
    }

    [UnitDefinitionClass]
    public static class ForceUnits
    {
        public static readonly Unit Newton = new Unit("newton", "N", LengthUnits.Meter * MassUnits.KiloGram * TimeUnits.Second.Power(-2));
    }

    [UnitDefinitionClass]
    public static class ElectricUnits
    {
        public static readonly Unit Ampere = new Unit("ampere", "amp", SIUnitTypes.ElectricCurrent);
        public static readonly Unit Coulomb = new Unit("coulomb", "C", TimeUnits.Second * Ampere);
        public static readonly Unit Volt = new Unit("volt", "V", EnergyUnits.Watt / Ampere);
        public static readonly Unit Ohm = new Unit("ohm", "Ω", Volt / Ampere);
        public static readonly Unit Farad = new Unit("farad", "F", Coulomb / Volt);
    }

    [UnitDefinitionClass]
    public static class EnergyUnits
    {
        public static readonly Unit Joule = new Unit("joule", "J", LengthUnits.Meter.Power(2) * MassUnits.KiloGram * TimeUnits.Second.Power(-2));
        public static readonly Unit KiloJoule = new Unit("kilojoule", "kJ", 1000.0 * Joule);
        public static readonly Unit MegaJoule = new Unit("megajoule", "MJ", 1000000.0 * Joule);
        public static readonly Unit GigaJoule = new Unit("gigajoule", "GJ", 1000000000.0 * Joule);

        public static readonly Unit Watt = new Unit("watt", "W", Joule / TimeUnits.Second);
        public static readonly Unit KiloWatt = new Unit("kilowatt", "kW", 1000.0 * Watt);
        public static readonly Unit MegaWatt = new Unit("megawatt", "MW", 1000000.0 * Watt);

        public static readonly Unit WattSecond = new Unit("watt-second", "Wsec", Watt * TimeUnits.Second);
        public static readonly Unit WattHour = new Unit("watt-hour", "Wh", Watt * TimeUnits.Hour);
        public static readonly Unit KiloWattHour = new Unit("kilowatt-hour", "kWh", 1000.0 * WattHour);

        public static readonly Unit Calorie = new Unit("calorie", "cal", 4.1868 * Joule);
        public static readonly Unit KiloCalorie = new Unit("kilocalorie", "kcal", 1000.0 * Calorie);

        public static readonly Unit HorsePower = new Unit("horsepower", "hp", 0.73549875 * KiloWatt);
    }

    [UnitDefinitionClass, UnitConversionClass]
    public static class TemperatureUnits
    {
        public static readonly Unit Kelvin = new Unit("Kelvin", "K", SIUnitTypes.ThermodynamicTemperature);
        public static readonly Unit DegreeCelcius = new Unit("degree celcius", "°C", new UnitType("celcius temperature"));
        public static readonly Unit DegreeFahrenheit = new Unit("degree fahrenheit", "°F", new UnitType("fahrenheit temperature"));

        #region Conversion functions

        public static void RegisterConversions()
        {
            // Register conversion functions:

            // Convert Celcius to Fahrenheit:
            UnitManager.RegisterConversion(DegreeCelcius, DegreeFahrenheit, delegate(Amount amount)
            {
                return new Amount(amount.Value * 9.0 / 5.0 + 32.0, DegreeFahrenheit);
            }
            );

            // Convert Fahrenheit to Celcius:
            UnitManager.RegisterConversion(DegreeFahrenheit, DegreeCelcius, delegate(Amount amount)
            {
                return new Amount((amount.Value - 32.0) / 9.0 * 5.0, DegreeCelcius);
            }
            );

            // Convert Celcius to Kelvin:
            UnitManager.RegisterConversion(DegreeCelcius, Kelvin, delegate(Amount amount)
            {
                return new Amount(amount.Value + 273.15, Kelvin);
            }
            );

            // Convert Kelvin to Celcius:
            UnitManager.RegisterConversion(Kelvin, DegreeCelcius, delegate(Amount amount)
            {
                return new Amount(amount.Value - 273.15, DegreeCelcius);
            }
            );

            // Convert Fahrenheit to Kelvin:
            UnitManager.RegisterConversion(DegreeFahrenheit, Kelvin, delegate(Amount amount)
            {
                return amount.ConvertedTo(DegreeCelcius).ConvertedTo(Kelvin);
            }
            );

            // Convert Kelvin to Fahrenheit:
            UnitManager.RegisterConversion(Kelvin, DegreeFahrenheit, delegate(Amount amount)
            {
                return amount.ConvertedTo(DegreeCelcius).ConvertedTo(DegreeFahrenheit);
            }
            );
        }

        #endregion Conversion functions
    }

    [UnitDefinitionClass]
    public static class PressureUnits
    {
        public static readonly Unit Pascal = new Unit("pascal", "Pa", ForceUnits.Newton * LengthUnits.Meter.Power(-2));
        public static readonly Unit HectoPascal = new Unit("hectopascal", "hPa", 100.0 * Pascal);
        public static readonly Unit KiloPascal = new Unit("kilopascal", "KPa", 1000.0 * Pascal);
        public static readonly Unit Bar = new Unit("bar", "bar", 100000.0 * Pascal);
        public static readonly Unit MilliBar = new Unit("millibar", "mbar", 0.001 * Bar);
        public static readonly Unit Atmosphere = new Unit("atmosphere", "atm", 101325.0 * Pascal);
    }

    [UnitDefinitionClass]
    public static class FrequencyUnits
    {
        public static readonly Unit Hertz = new Unit("Hertz", "hz", TimeUnits.Second.Power(-1));
        public static readonly Unit MegaHerts = new Unit("MegaHertz", "Mhz", 1000000.0 * Hertz);
        public static readonly Unit RPM = new Unit("Rounds per minute", "rpm", TimeUnits.Minute.Power(-1));
    }

    [UnitDefinitionClass]
    public static class AmountOfSubstanceUnits
    {
        public static readonly Unit Mole = new Unit("mole", "mol", SIUnitTypes.AmountOfSubstance);
    }

    public static class LuminousIntensityUnits
    {
        public static readonly Unit Candela = new Unit("candela", "cd", SIUnitTypes.LuminousIntensity);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rudi Breedenraedt
Architect Wolters Kluwer Belgium
Belgium Belgium
Rudi is a Software Architect at Wolters Kluwer Belgium.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 17 Jul 2013
Article Copyright 2013 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid