Skip to content

Commit ab6bd32

Browse files
tmilnthorpangularsen
authored andcommitted
Fix negative FeetInches and StonePounds with negative values #574
Fix negative FeetInches and StonePounds with negative values
1 parent 2555377 commit ab6bd32

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

UnitsNet.Tests/CustomCode/LengthTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,21 @@ public void MinValueIsCorrectForUnitWithBaseTypeDouble()
153153
{
154154
Assert.Equal(double.MinValue, Length.MinValue.Meters);
155155
}
156+
157+
[Fact]
158+
public void NegativeLengthToStonePoundsReturnsCorrectValues()
159+
{
160+
var negativeLength = Length.FromInches(-1.0);
161+
var feetInches = negativeLength.FeetInches;
162+
163+
Assert.Equal(0, feetInches.Feet);
164+
Assert.Equal(-1.0, feetInches.Inches);
165+
166+
negativeLength = Length.FromInches(-25.0);
167+
feetInches = negativeLength.FeetInches;
168+
169+
Assert.Equal(-2.0, feetInches.Feet);
170+
Assert.Equal(-1.0, feetInches.Inches);
171+
}
156172
}
157173
}

UnitsNet.Tests/CustomCode/MassTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// THE SOFTWARE.
2121

2222
using System;
23+
using UnitsNet.Units;
2324
using Xunit;
2425

2526
namespace UnitsNet.Tests.CustomCode
@@ -106,5 +107,21 @@ public void MassTimesAccelerationEqualsForce()
106107
Force force = Mass.FromKilograms(18)*Acceleration.FromMetersPerSecondSquared(3);
107108
Assert.Equal(force, Force.FromNewtons(54));
108109
}
110+
111+
[Fact]
112+
public void NegativeMassToStonePoundsReturnsCorrectValues()
113+
{
114+
var negativeMass = Mass.FromPounds(-1.0);
115+
var stonePounds = negativeMass.StonePounds;
116+
117+
Assert.Equal(0, stonePounds.Stone);
118+
Assert.Equal(-1.0, stonePounds.Pounds);
119+
120+
negativeMass = Mass.FromPounds(-25.0);
121+
stonePounds = negativeMass.StonePounds;
122+
123+
Assert.Equal(-1.0, stonePounds.Stone);
124+
Assert.Equal(-11.0, stonePounds.Pounds);
125+
}
109126
}
110127
}

UnitsNet/CustomCode/Quantities/Length.extra.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public sealed partial class Length
4242
public partial struct Length
4343
#endif
4444
{
45-
private const double FeetToInches = 12;
45+
private const double InchesInOneFoot = 12;
4646

4747
/// <summary>
4848
/// Converts the length to a customary feet/inches combination.
@@ -51,11 +51,11 @@ public FeetInches FeetInches
5151
{
5252
get
5353
{
54-
double totalInches = Inches;
55-
double wholeFeet = Math.Floor(totalInches/FeetToInches);
56-
double inches = totalInches%FeetToInches;
54+
var inInches = Inches;
55+
var feet = Math.Truncate(inInches / InchesInOneFoot);
56+
var inches = inInches % InchesInOneFoot;
5757

58-
return new FeetInches(wholeFeet, inches);
58+
return new FeetInches(feet, inches);
5959
}
6060
}
6161

@@ -64,7 +64,7 @@ public FeetInches FeetInches
6464
/// </summary>
6565
public static Length FromFeetInches(double feet, double inches)
6666
{
67-
return FromInches(FeetToInches*feet + inches);
67+
return FromInches(InchesInOneFoot*feet + inches);
6868
}
6969

7070
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx

UnitsNet/CustomCode/Quantities/Mass.extra.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static Mass FromGravitationalForce(Force f)
5050
/// StonePounds related code makes it easier to work with Stone/Pound combination, which are customarily used in the UK
5151
/// to express body weight. For example, someone weighs 11 stone 4 pounds (about 72 kilograms).
5252
/// </summary>
53-
private const double StoneToPounds = 14;
53+
private const double StonesInOnePound = 14.0;
5454

5555
/// <summary>
5656
/// Converts the mass to a customary stone/pounds combination.
@@ -59,11 +59,12 @@ public StonePounds StonePounds
5959
{
6060
get
6161
{
62-
double totalPounds = Pounds;
63-
double wholeStone = Math.Floor(totalPounds/StoneToPounds);
64-
double pounds = totalPounds%StoneToPounds;
62+
var inPounds = Pounds;
6563

66-
return new StonePounds(wholeStone, pounds);
64+
var stones = Math.Truncate(inPounds / StonesInOnePound);
65+
var pounds = inPounds % StonesInOnePound;
66+
67+
return new StonePounds(stones, pounds);
6768
}
6869
}
6970

@@ -72,7 +73,7 @@ public StonePounds StonePounds
7273
/// </summary>
7374
public static Mass FromStonePounds(double stone, double pounds)
7475
{
75-
return FromPounds(StoneToPounds*stone + pounds);
76+
return FromPounds(StonesInOnePound*stone + pounds);
7677
}
7778

7879
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx

0 commit comments

Comments
 (0)