Skip to content

Commit

Permalink
Added short and long tons.
Browse files Browse the repository at this point in the history
  • Loading branch information
distantcam committed Jul 22, 2013
1 parent 6fb90b8 commit 4347990
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
20 changes: 20 additions & 0 deletions Src/UnitsNet/Mass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Src/UnitsNet/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public enum Unit
//Microgram,
//Nanogram,

// Mass (imperial)
ShortTon,
LongTon,

// Pressure
Pascal,
KiloPascal,
Expand Down
14 changes: 14 additions & 0 deletions Src/UnitsNet/UnitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.",
Expand Down
12 changes: 10 additions & 2 deletions Tests/MassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ namespace UnitsNet.Tests.net35
{
[TestFixture]
public class MassTests
{
{
private const double Delta = 1E-5;

[Test]
public void KilogramToMassUnits()
{
Expand All @@ -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]
Expand All @@ -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);
}
}
}

0 comments on commit 4347990

Please sign in to comment.