Skip to content
Expansible Unit Conversion Library for .Net Core and .Net Framework
C#
Branch: master
Clone or download
Latest commit 6e635b4 Dec 14, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Soft update after transfer of repo ownsership. Closes #16 Feb 10, 2018
Logo Uploaded logo Feb 11, 2018
UnitConversion Added .StyleCop file per project instead of global rule set Dec 12, 2018
UnitConversionExample Added .StyleCop file per project instead of global rule set Dec 12, 2018
UnitConversionTests Added .StyleCop file per project instead of global rule set Dec 12, 2018
.editorconfig Added ..editorconfig, code of conduct, contributing guidelines and re… Feb 8, 2018
.gitignore Added ..editorconfig, code of conduct, contributing guidelines and re… Feb 8, 2018
LICENSE.md Added ..editorconfig, code of conduct, contributing guidelines and re… Feb 8, 2018
NuGet.Config Add Nuget.config to solution Aug 19, 2016
Readme.md
UnitConversion.sln Added .StyleCop file per project instead of global rule set Dec 12, 2018
_config.yml Set theme jekyll-theme-cayman Jan 23, 2018
appveyor.yml Update appveyor.yml Dec 14, 2018
stylecop.json

Readme.md

Unit Conversion

Build status NuGet MIT License Gitter chat

What is it?

An expansible .Net class library with support for all modern platforms

  • .Net Framework 4.0+
  • .Net Standard 1.3+ (.Net 4.5.1+ and .Net Core)

UnitConversion is designed to be expansible through factories or through concrete converter implementations.

Implementations are currently limited, but include:

Pull requests with custom implementations are welcome! Please have a look through the contributing guidelines and our code of conduct.

Example

double kgValue;
double lbValue;

// Simple programmatic approach to conversion, using string `Synonyms`
var kgToLbs = new MassConverter("kg", "lbs");
kgValue = 452;
lbValue = kgToLbs.LeftToRight(kgValue);
Console.WriteLine("452kg in pounds is " + lbValue);

// Converting both ways is easy
kgValue = kgToLbs.RightToLeft(lbValue);
Console.WriteLine("Converted back: " + kgValue);

// Rounding is part of conversion
lbValue = kgToLbs.LeftToRight(kgValue, 2);
Console.WriteLine("452kg in pounds (to 2 decimal places) is " + lbValue);

// You can easily customise converters to support Synonyms used in
// business logic, such as those stored on a database
kgToLbs.AddSynonym("kg", "MTOW (KG)");

kgValue = 3000;
kgToLbs.UnitLeft = "MTOW (KG)";
lbValue = kgToLbs.LeftToRight(kgValue);
Console.WriteLine("3000 MTOW (KG) in lbs is " + lbValue);

// Add a new unit with a custom conversion factor
kgToLbs.AddUnit("Chuck Norris", 9001);
kgToLbs.UnitRight = "Chuck Norris";
kgValue = 7;
var chucks = kgToLbs.LeftToRight(kgValue);
Console.WriteLine("7kg is equal to " + lbValue + " chucks");

Converters are easy to define and contribute to:

public class DistanceConverter : BaseUnitConverter 
{
    UnitFactors units = new UnitFactors("m") 
    {
        { new UnitFactorSynonyms("m", "metre"), 1 },
        { new UnitFactorSynonyms("km", "kilometre"), 0.001 },
        { new UnitFactorSynonyms("cm", "centimetre"), 100 },
        { new UnitFactorSynonyms("mm", "millimetre"), 1000 },
        { new UnitFactorSynonyms("ft", "foot", "feet"), 1250d / 381 },
        { new UnitFactorSynonyms("yd", "yard"), 1250d / 1143 },
        { "mile", 125d / 201168 }, // strings automatically cast to UnitFactorSynonyms
        { new UnitFactorSynonyms("in", "inch"), 5000d / 127 },
    };

    public DistanceConverter(string leftUnit, string rightUnit) 
    {
        Instantiate(units, leftUnit, rightUnit);
    }
    public DistanceConverter() 
    {
        Instantiate(units);
    }
}

Current Maintainer

Previous maintainers

Many thanks to the original maintainers @Nick-Lucas and @gkampolis for all their hard work.

You can’t perform that action at this time.