From 43479907e2a3a892ad51f2716bcb5f4fe1d2fd5b Mon Sep 17 00:00:00 2001 From: Cameron MacFarland Date: Mon, 22 Jul 2013 14:49:00 +0800 Subject: [PATCH] Added short and long tons. --- Src/UnitsNet/Mass.cs | 20 ++++++++++++++++++++ Src/UnitsNet/Unit.cs | 4 ++++ Src/UnitsNet/UnitConverter.cs | 14 ++++++++++++++ Tests/MassTests.cs | 12 ++++++++++-- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/Src/UnitsNet/Mass.cs b/Src/UnitsNet/Mass.cs index 75f945494c..e6ea02bfa3 100644 --- a/Src/UnitsNet/Mass.cs +++ b/Src/UnitsNet/Mass.cs @@ -39,6 +39,16 @@ private Mass(double kilograms) Kilograms = kilograms; } + public double ShortTons + { + get { return Kilograms*907.18474; } + } + + public double LongTons + { + get { return Kilograms*1016.0469088; } + } + public double Megatonnes { get { return Kilograms*1E-9; } @@ -141,6 +151,16 @@ public static Mass FromGravitationalForce(Force f) return new Mass(f.KilogramForce); } + public static Mass FromLongTons(double value) + { + return new Mass(value*0.000984207); + } + + public static Mass FromShortTons(double value) + { + return new Mass(value*0.00110231); + } + #endregion #region Arithmetic operators diff --git a/Src/UnitsNet/Unit.cs b/Src/UnitsNet/Unit.cs index 1b70ac422b..bc51fab98d 100644 --- a/Src/UnitsNet/Unit.cs +++ b/Src/UnitsNet/Unit.cs @@ -54,6 +54,10 @@ public enum Unit //Microgram, //Nanogram, + // Mass (imperial) + ShortTon, + LongTon, + // Pressure Pascal, KiloPascal, diff --git a/Src/UnitsNet/UnitConverter.cs b/Src/UnitsNet/UnitConverter.cs index 84f7955000..eeb27e8a36 100644 --- a/Src/UnitsNet/UnitConverter.cs +++ b/Src/UnitsNet/UnitConverter.cs @@ -257,6 +257,14 @@ private static bool TryConvertMass(double value, Unit fromUnit, Unit toUnit, out return true; //case Unit.Microgram: //case Unit.Nanogram: + + case Unit.ShortTon: + newValue = Convert(Mass.FromShortTons(value), toUnit); + return true; + case Unit.LongTon: + newValue = Convert(Mass.FromLongTons(value), toUnit); + return true; + default: newValue = 0; return false; @@ -325,6 +333,12 @@ private static double Convert(Mass m, Unit toUnit) // return m.Micrograms; //case Unit.Nanogram: // return m.Nanograms; + + case Unit.ShortTon: + return m.ShortTons; + case Unit.LongTon: + return m.LongTons; + default: throw new Exception( string.Format("Conversion from mass to unit [{0}] is either not valid or not yet implemented.", diff --git a/Tests/MassTests.cs b/Tests/MassTests.cs index a83058754e..92291b0ec3 100644 --- a/Tests/MassTests.cs +++ b/Tests/MassTests.cs @@ -4,7 +4,9 @@ namespace UnitsNet.Tests.net35 { [TestFixture] public class MassTests - { + { + private const double Delta = 1E-5; + [Test] public void KilogramToMassUnits() { @@ -20,6 +22,9 @@ public void KilogramToMassUnits() Assert.AreEqual(1E4, oneKg.Decigrams); Assert.AreEqual(1E5, oneKg.Centigrams); Assert.AreEqual(1E6, oneKg.Milligrams); + + Assert.AreEqual(907.18474, oneKg.ShortTons, Delta); + Assert.AreEqual(1016.0469088, oneKg.LongTons, Delta); } [Test] @@ -35,7 +40,10 @@ public void MassUnitsRoundTrip() Assert.AreEqual(1, Mass.FromGrams(oneKg.Grams).Kilograms); Assert.AreEqual(1, Mass.FromDecigrams(oneKg.Decigrams).Kilograms); Assert.AreEqual(1, Mass.FromCentigrams(oneKg.Centigrams).Kilograms); - Assert.AreEqual(1, Mass.FromMilligrams(oneKg.Milligrams).Kilograms); + Assert.AreEqual(1, Mass.FromMilligrams(oneKg.Milligrams).Kilograms); + + Assert.AreEqual(1, Mass.FromShortTons(oneKg.ShortTons).Kilograms, Delta); + Assert.AreEqual(1, Mass.FromLongTons(oneKg.LongTons).Kilograms, Delta); } } } \ No newline at end of file