This project is read-only.

Check the predefined units from here: Link

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

This examples are showing the usage of library:

//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 4, 2014 at 9:01 AM by epsi1on, version 3