diff --git a/UnitsNet.Tests/CustomCode/LengthTests.cs b/UnitsNet.Tests/CustomCode/LengthTests.cs index 83faafb8c1..df789dbdca 100644 --- a/UnitsNet.Tests/CustomCode/LengthTests.cs +++ b/UnitsNet.Tests/CustomCode/LengthTests.cs @@ -153,5 +153,21 @@ public void MinValueIsCorrectForUnitWithBaseTypeDouble() { Assert.Equal(double.MinValue, Length.MinValue.Meters); } + + [Fact] + public void NegativeLengthToStonePoundsReturnsCorrectValues() + { + var negativeLength = Length.FromInches(-1.0); + var feetInches = negativeLength.FeetInches; + + Assert.Equal(0, feetInches.Feet); + Assert.Equal(-1.0, feetInches.Inches); + + negativeLength = Length.FromInches(-25.0); + feetInches = negativeLength.FeetInches; + + Assert.Equal(-2.0, feetInches.Feet); + Assert.Equal(-1.0, feetInches.Inches); + } } } diff --git a/UnitsNet.Tests/CustomCode/MassTests.cs b/UnitsNet.Tests/CustomCode/MassTests.cs index 9cbc92c2de..6b0a9b70ed 100644 --- a/UnitsNet.Tests/CustomCode/MassTests.cs +++ b/UnitsNet.Tests/CustomCode/MassTests.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using UnitsNet.Units; using Xunit; namespace UnitsNet.Tests.CustomCode @@ -106,5 +107,21 @@ public void MassTimesAccelerationEqualsForce() Force force = Mass.FromKilograms(18)*Acceleration.FromMetersPerSecondSquared(3); Assert.Equal(force, Force.FromNewtons(54)); } + + [Fact] + public void NegativeMassToStonePoundsReturnsCorrectValues() + { + var negativeMass = Mass.FromPounds(-1.0); + var stonePounds = negativeMass.StonePounds; + + Assert.Equal(0, stonePounds.Stone); + Assert.Equal(-1.0, stonePounds.Pounds); + + negativeMass = Mass.FromPounds(-25.0); + stonePounds = negativeMass.StonePounds; + + Assert.Equal(-1.0, stonePounds.Stone); + Assert.Equal(-11.0, stonePounds.Pounds); + } } } diff --git a/UnitsNet/CustomCode/Quantities/Length.extra.cs b/UnitsNet/CustomCode/Quantities/Length.extra.cs index 8978af0364..7d9ba655d8 100644 --- a/UnitsNet/CustomCode/Quantities/Length.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Length.extra.cs @@ -42,7 +42,7 @@ public sealed partial class Length public partial struct Length #endif { - private const double FeetToInches = 12; + private const double InchesInOneFoot = 12; /// /// Converts the length to a customary feet/inches combination. @@ -51,11 +51,11 @@ public FeetInches FeetInches { get { - double totalInches = Inches; - double wholeFeet = Math.Floor(totalInches/FeetToInches); - double inches = totalInches%FeetToInches; + var inInches = Inches; + var feet = Math.Truncate(inInches / InchesInOneFoot); + var inches = inInches % InchesInOneFoot; - return new FeetInches(wholeFeet, inches); + return new FeetInches(feet, inches); } } @@ -64,7 +64,7 @@ public FeetInches FeetInches /// public static Length FromFeetInches(double feet, double inches) { - return FromInches(FeetToInches*feet + inches); + return FromInches(InchesInOneFoot*feet + inches); } // Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx diff --git a/UnitsNet/CustomCode/Quantities/Mass.extra.cs b/UnitsNet/CustomCode/Quantities/Mass.extra.cs index 3811be83dc..11fb195c63 100644 --- a/UnitsNet/CustomCode/Quantities/Mass.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Mass.extra.cs @@ -50,7 +50,7 @@ public static Mass FromGravitationalForce(Force f) /// StonePounds related code makes it easier to work with Stone/Pound combination, which are customarily used in the UK /// to express body weight. For example, someone weighs 11 stone 4 pounds (about 72 kilograms). /// - private const double StoneToPounds = 14; + private const double StonesInOnePound = 14.0; /// /// Converts the mass to a customary stone/pounds combination. @@ -59,11 +59,12 @@ public StonePounds StonePounds { get { - double totalPounds = Pounds; - double wholeStone = Math.Floor(totalPounds/StoneToPounds); - double pounds = totalPounds%StoneToPounds; + var inPounds = Pounds; - return new StonePounds(wholeStone, pounds); + var stones = Math.Truncate(inPounds / StonesInOnePound); + var pounds = inPounds % StonesInOnePound; + + return new StonePounds(stones, pounds); } } @@ -72,7 +73,7 @@ public StonePounds StonePounds /// public static Mass FromStonePounds(double stone, double pounds) { - return FromPounds(StoneToPounds*stone + pounds); + return FromPounds(StonesInOnePound*stone + pounds); } // Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx