This project is read-only.

Introduction and how to use

For more information please see the "UnitConversionLib: Smart Unit Conversion Library in c#" article on the codeproject.com:

http://www.codeproject.com/Articles/787029/UnitConversionLib-Smart-Unit-Conversion-Library-in

Project Description

UnitConversionLib is a smart unit conversion library with some cool features and lets you change units and also do mathematical operations like * and / and ^ and + between measurement object. for example it lets you to divide '100 m' (equals to 100 meters) with '10 sec' (10 seconds) and will show you result is '10 m/s'.
<!-- code formatted by http://manoli.net/csharpformat/ -->

var D = Measurable.Parse("100 m");
var T = Measurable.Parse("10 sec");
var V = D/T;
Console.WriteLine("{0:0.0}",V); // will show 10.0 m/sec

 

Also will tell you its equal to 118109.999 foot/hr:

Console.WriteLine("{0:0.0}",V.ConvertTo("foot/hour")); // will show 118109.9 foot/hour

It included a brief symbolic math engine inside and uses that for many operation. You can also make any new unit.

Check the examples:

Check the predefined units from here: Link

<!-- code formatted by http://manoli.net/csharpformat/ -->

//Creating and parsing units
Unit unt0 = Unit.GetRegisteredUnit("N");//From registered library
Unit unt1 = Unit.Parse("N");//Parsing from string - simple format
Unit unt2 = "N";//Explicit conversion
Unit unt3 = Unit.Parse("kg*m/sec^2");//Parsing from string - from expression (1)
Unit unt4 = Unit.Parse("kg*m/s^(ton/kg/500)");//Parsing from string - from expression (2)
Unit unt5 = Unit.Parse("kg*m/s^(kg/kg*2)");//Parsing from string - from expression (3)
Unit unt6 = Unit.Parse("kg*m") / (Unit.Parse("sec") ^ 2);//Parsing from string - from expression (4)

Unit kg = Unit.Parse("kg");
Unit m = Unit.Parse("m");
Unit sec = Unit.Parse("sec");

Unit unt7 = kg * m / (sec ^ 2);//Evaluating thro runtime

Debug.Assert(unt0 == unt1 && unt1 == unt2 && unt2 == unt3 && unt3 == unt4 && unt4 == unt5 && unt5 == unt6 && unt6 == unt7);

Unit unt8 = Unit.Parse("kg*m/s^(kg)");//will raise error, a unit cannot be placed in power

//In Action I:
Measurable meu0 = Measurable.Parse("100 N");
Measurable meu1 = "100 N";
Measurable meu2 = new Measurable(unt0, 100);

Debug.Assert(meu0 == meu1);

Measurable meu3 = meu0.ConvertTo("kgf");
Debug.Assert(meu3.Amount == 1000);
Debug.Assert(meu3.Unit == "kgf");

//In action II, calculating average acceleration

Measurable deltax = "100 m";
Measurable deltaT = "10 sec";
Measurable scUnit = "2 kg/kg";

Measurable avgAccl = deltax / (deltaT ^ scUnit);//which is 1 m/sec^2

Debug.Assert(avgAccl.Amount == 1);
Debug.Assert(avgAccl.Unit == "m/  sec^2");


//In action III: creating new units
Unit bit = Unit.CreateNew("b");
Unit B = bit * 8;
Unit.Register(ref B, "B");
Unit KB = B * 1024;
Unit.Register(ref KB, "KB");
Unit MB = KB * 1024;
Unit.Register(ref MB, "MB");
Unit GB = MB * 1024;
Unit.Register(ref GB, "GB");
Unit TB = GB * 1024;
Unit.Register(ref TB, "TB");

Unit bps = bit / Unit.GetRegisteredUnit("sec");
Unit.Register(ref bps, "bps");
Measurable smb = "100 MB/sec";
Measurable sb = smb.ConvertTo("bps");// 100 MB = 100*1024*1024*8 b = 838860800 b

Debug.Assert(sb.Amount == 838860800);
Debug.Assert(sb.Unit == bit / "sec");


 

Other .Net libraries and utilities related to unit conversion, which some have advantages (and disadvantages) over this library:
http://unitsofmeasure.codeplex.com/
http://physicalmeasure.codeplex.com/
http://quantitysystem.codeplex.com/
http://unitconvertlibrary.codeplex.com/
http://units.codeplex.com/
http://ngenericdimensions.codeplex.com/
http://sourceforge.net/projects/quantitiesnet/
http://sourceforge.net/projects/unitcon/
http://www.codeproject.com/Articles/23087/Measurement-Unit-Conversion-Library
http://www.codeproject.com/Articles/404573/Units-of-Measure-Library-for-NET
http://www.codeproject.com/Articles/413750/Units-of-Measure-Validator-for-Csharp
http://www.codeproject.com/Articles/611731/Working-with-Units-and-Amounts
http://www.gnu.org/software/units/

Last edited Jun 24, 2014 at 9:44 PM by epsi1on, version 27