Giter Club home page Giter Club logo

unitconversion's Introduction

Build status NuGet MIT License

Important Note

Following January 2018, the project is maintained by @gkampolis. The project's new "home" can be found here.

At the moment, the transfer of maintenance is underway.

Unit Conversion

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!


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);
    }
}

Many thanks to the original maintener @Nick-Lucas for all his hard work.

unitconversion's People

Contributors

anovik avatar gkampolis avatar nick-lucas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.