Unit hour

Jun 20, 2011 at 2:07 PM

How can i represent a quantity with unit km/h or Watt hour ?

Coordinator
Jun 23, 2011 at 2:11 AM
Edited Aug 22, 2013 at 6:17 PM
Hi LNT Sorry for the late reply.

You can some thing like this:

PhysicalQuantity KmPrHour_example1 = PhysicalMeasure.Prefix.K * SI.m / SI.h;
PhysicalQuantity Speed1 = 123 * KmPrHour_example1;

IPhysicalUnitKm = PhysicalMeasure.PhysicalUnit.Parse("Km");
IPhysicalUnit h = PhysicalMeasure.PhysicalUnit.Parse("h");
IPhysicalQuantity KmPrHour_example2 = Km.Divide(h);
PhysicalQuantity Speed2 = new PhysicalQuantity (123, KmPrHour_example2);

string KmPrHour_Str = "Km/h" ;
string Tempstr = KmPrHour_Str;
IPhysicalUnit KmPrHour_example3 = PhysicalMeasure.PhysicalUnit.ParseUnit(ref Tempstr);
PhysicalQuantity Speed3 = new PhysicalQuantity (123, KmPrHour_example3);
PhysicalQuantity expected = new PhysicalQuantity(123 * 1000.0/(60 * 60) , SI.m / SI .s);

Assert.AreEqual(expected, Speed1);
Assert.AreEqual(expected, Speed2);
Assert.AreEqual(expected, Speed3);

IPhysicalQuantity WattHour_example1 = SI.W * SI.h;
PhysicalUnit WattHour_example2 = (PhysicalUnit)new ConvertibleUnit("WattHour", "Wh", SI.J, new ScaledValueConversion(1.0 / 3600)); /* [Wh] = 1/3600 * [J] */
PhysicalQuantity E_1 = new PhysicalQuantity(1, WattHour_example1 ); // 1 Wh
PhysicalQuantity E_2 = new PhysicalQuantity(0.001, Prefix.K * WattHour_example2); // 0.001 KWh
PhysicalQuantity expected = new PhysicalQuantity(3600, SI.J);
Assert.AreEqual(expected, E_1);
Assert.AreEqual(expected, E_2);

I hope you can use it.
Apr 16, 2013 at 3:17 PM
Thanks KBravo, very useful stuff. Can we expect widely used conversion functions like kWh to be added or is that always up to the user to calculate from the base and/or derived unit set?

Great code!
GG
Coordinator
Apr 16, 2013 at 9:05 PM
Hi GGLeGrand

Thanks for the nice words. What kind of conversions would you like to have? I have not planed any more conversion functions, as I hope that ConvertTo() should be able to handle the conversion between any units. If you have some case that is general usefull I could add support for it

/KiloBravo