Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions UnitsNet.Tests/InterUnitConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// THE SOFTWARE.

using NUnit.Framework;
using System;

namespace UnitsNet.Tests
{
Expand All @@ -41,5 +42,41 @@ public void KilogramToKilogramForce()
Force force = Force.FromKilogramsForce(mass.Kilograms);
Assert.AreEqual(mass.Kilograms, force.KilogramsForce);
}

[Test]
public void LengthAndTimeSpanToSpeed()
{
Length length = Length.FromMeters(10);
Speed speed = length / TimeSpan.FromSeconds(1);
Assert.AreEqual(10.0, speed.MetersPerSecond);
}

[Test]
public void LengthAndLengthToArea()
{
Length length1 = Length.FromMeters(10);
Length length2 = Length.FromMeters(20);
Area area = length1 * length2;
Assert.AreEqual(200.0, area.SquareMeters);
}

[Test]
public void LengthAndAreaToVolume()
{
Length length = Length.FromMeters(10);
Area area = Area.FromSquareMeters(20);
Volume volume = length * area;
Assert.AreEqual(200.0, volume.CubicMeters);
}


[Test]
public void LengthAndForceToEnergy()
{
Length length = Length.FromMeters(10);
Force force = Force.FromNewtons(20);
Energy energy = length * force;
Assert.AreEqual(200.0, energy.Joules);
}
}
}
16 changes: 16 additions & 0 deletions UnitsNet/GeneratedCode/UnitClasses/Length.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,22 @@ public static string GetAbbreviation(LengthUnit unit, CultureInfo culture = null
}

#endregion
public static Speed operator / (Length left, TimeSpan right)
{
return Speed.FromMetersPerSecond(left.Meters / right.TotalSeconds);
}
public static Area operator * (Length left, Length right)
{
return Area.FromSquareMeters(left.Meters * right.Meters);
}
public static Volume operator * (Length left, Area right)
{
return Volume.FromCubicMeters(left.Meters * right.SquareMeters);
}
public static Energy operator * (Length left, Force right)
{
return Energy.FromJoules(left.Meters * right.Newtons);
}

#region Equality / IComparable

Expand Down
16 changes: 15 additions & 1 deletion UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function GenerateUnitClassSourceCode($unitClass)
$baseUnitPluralNameLower = $baseUnitPluralName.ToLowerInvariant()
$unitEnumName = "$className" + "Unit";
$baseUnitFieldName = "_"+[Char]::ToLowerInvariant($baseUnitPluralName[0]) + $baseUnitPluralName.Substring(1);
$operatorOverloads = $unitClass.OperatorOverloads;

@"
// Copyright � 2007 by Initial Force AS. All rights reserved.
Expand Down Expand Up @@ -180,7 +181,20 @@ namespace UnitsNet
}

#endregion
"@; }@"
"@; }
foreach ($operatorOverload in $operatorOverloads) {

$returnUnit = $operatorOverload.ReturnUnit;
$operator = $operatorOverload.Operator;
$returnUnitBasePluralName = $operatorOverload.ReturnUnitBasePluralName;
$otherUnitBasePluralName = $operatorOverload.OtherUnitBasePluralName;
@"
public static $returnUnit operator $operator ($className left, $($operatorOverload.OtherUnit) right)
{
return $returnUnit.From$returnUnitBasePluralName(left.$baseUnitPluralName $operator right.$otherUnitBasePluralName);
}
"@; }
@"

#region Equality / IComparable

Expand Down
240 changes: 135 additions & 105 deletions UnitsNet/Scripts/UnitDefinitions/Length.json
Original file line number Diff line number Diff line change
@@ -1,121 +1,151 @@
{
"Name": "Length",
"BaseUnit": "Meter",
"XmlDoc": "Many different units of length have been used around the world. The main units in modern use are U.S. customary units in the United States and the Metric system elsewhere. British Imperial units are still used for some purposes in the United Kingdom and some other countries. The metric system is sub-divided into SI and non-SI units.",
"Units": [
"Name": "Length",
"BaseUnit": "Meter",
"XmlDoc": "Many different units of length have been used around the world. The main units in modern use are U.S. customary units in the United States and the Metric system elsewhere. British Imperial units are still used for some purposes in the United Kingdom and some other countries. The metric system is sub-divided into SI and non-SI units.",
"Units": [
{
"SingularName": "Meter",
"PluralName": "Meters",
"FromUnitToBaseFunc": "x",
"FromBaseToUnitFunc": "x",
"Prefixes": [ "Nano", "Micro", "Milli", "Centi", "Deci", "Kilo" ],
"Localization": [
{
"SingularName": "Meter",
"PluralName": "Meters",
"FromUnitToBaseFunc": "x",
"FromBaseToUnitFunc": "x",
"Prefixes": ["Nano","Micro","Milli","Centi","Deci","Kilo"],
"Localization": [
{
"Culture": "en-US",
"Abbreviations": ["m"]
},
{
"Culture": "ru-RU",
"Abbreviations": ["м"],
"AbbreviationsWithPrefixes": ["нм","мкм","мм","см","дм","км"]
}
]
"Culture": "en-US",
"Abbreviations": [ "m" ]
},
{
"SingularName": "Mile",
"PluralName": "Miles",
"FromUnitToBaseFunc": "x*1609.34",
"FromBaseToUnitFunc": "x/1609.34",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": ["mi"]
},
{
"Culture": "ru-RU",
"Abbreviations": ["миля"]
}
]
"Culture": "ru-RU",
"Abbreviations": [ "м" ],
"AbbreviationsWithPrefixes": [ "нм", "мкм", "мм", "см", "дм", "км" ]
}
]
},
{
"SingularName": "Mile",
"PluralName": "Miles",
"FromUnitToBaseFunc": "x*1609.34",
"FromBaseToUnitFunc": "x/1609.34",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "mi" ]
},
{
"SingularName": "Yard",
"PluralName": "Yards",
"FromUnitToBaseFunc": "x*0.9144",
"FromBaseToUnitFunc": "x/0.9144",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": ["yd"]
},
{
"Culture": "ru-RU",
"Abbreviations": ["ярд"]
}
]
"Culture": "ru-RU",
"Abbreviations": [ "миля" ]
}
]
},
{
"SingularName": "Yard",
"PluralName": "Yards",
"FromUnitToBaseFunc": "x*0.9144",
"FromBaseToUnitFunc": "x/0.9144",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "yd" ]
},
{
"Culture": "ru-RU",
"Abbreviations": [ "ярд" ]
}
]
},
{
"SingularName": "Foot",
"PluralName": "Feet",
"FromUnitToBaseFunc": "x*0.3048",
"FromBaseToUnitFunc": "x/0.3048",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "ft", "\\\'" ]
},
{
"SingularName": "Foot",
"PluralName": "Feet",
"FromUnitToBaseFunc": "x*0.3048",
"FromBaseToUnitFunc": "x/0.3048",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": ["ft","\\\'"]
},
{
"Culture": "ru-RU",
"Abbreviations": ["фут"]
}
]
"Culture": "ru-RU",
"Abbreviations": [ "фут" ]
}
]
},
{
"SingularName": "Inch",
"PluralName": "Inches",
"FromUnitToBaseFunc": "x*2.54e-2",
"FromBaseToUnitFunc": "x/2.54e-2",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "in", "\\\"" ]
},
{
"SingularName": "Inch",
"PluralName": "Inches",
"FromUnitToBaseFunc": "x*2.54e-2",
"FromBaseToUnitFunc": "x/2.54e-2",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": ["in","\\\""]
},
{
"Culture": "ru-RU",
"Abbreviations": ["дюйм"]
}
]
"Culture": "ru-RU",
"Abbreviations": [ "дюйм" ]
}
]
},
{
"SingularName": "Mil",
"PluralName": "Mils",
"FromUnitToBaseFunc": "x*2.54e-5",
"FromBaseToUnitFunc": "x/2.54e-5",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "mil" ]
},
{
"SingularName": "Mil",
"PluralName": "Mils",
"FromUnitToBaseFunc": "x*2.54e-5",
"FromBaseToUnitFunc": "x/2.54e-5",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": ["mil"]
},
{
"Culture": "ru-RU",
"Abbreviations": ["мил"]
}
]
"Culture": "ru-RU",
"Abbreviations": [ "мил" ]
}
]
},
{
"SingularName": "Microinch",
"PluralName": "Microinches",
"FromUnitToBaseFunc": "x*2.54e-8",
"FromBaseToUnitFunc": "x/2.54e-8",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "μin" ]
},
{
"SingularName": "Microinch",
"PluralName": "Microinches",
"FromUnitToBaseFunc": "x*2.54e-8",
"FromBaseToUnitFunc": "x/2.54e-8",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": ["μin"]
},
{
"Culture": "ru-RU",
"Abbreviations": ["микродюйм"]
}
]
"Culture": "ru-RU",
"Abbreviations": [ "микродюйм" ]
}
]
]
}
],
"OperatorOverloads": [
{
"Operator": "/",
"OtherUnit": "TimeSpan",
"ReturnUnit": "Speed",
"ReturnUnitBasePluralName": "MetersPerSecond",
"OtherUnitBasePluralName": "TotalSeconds"
},
{
"Operator": "*",
"OtherUnit": "Length",
"ReturnUnit": "Area",
"ReturnUnitBasePluralName": "SquareMeters",
"OtherUnitBasePluralName": "Meters"
},
{
"Operator": "*",
"OtherUnit": "Area",
"ReturnUnit": "Volume",
"ReturnUnitBasePluralName": "CubicMeters",
"OtherUnitBasePluralName": "SquareMeters"
},
{
"Operator": "*",
"OtherUnit": "Force",
"ReturnUnit": "Energy",
"ReturnUnitBasePluralName": "Joules",
"OtherUnitBasePluralName": "Newtons"
}
]
}