Skip to content

Fix negative FeetInches and StonePounds with negative values #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2018
Merged
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
16 changes: 16 additions & 0 deletions UnitsNet.Tests/CustomCode/LengthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
17 changes: 17 additions & 0 deletions UnitsNet.Tests/CustomCode/MassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// THE SOFTWARE.

using System;
using UnitsNet.Units;
using Xunit;

namespace UnitsNet.Tests.CustomCode
Expand Down Expand Up @@ -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);
}
}
}
12 changes: 6 additions & 6 deletions UnitsNet/CustomCode/Quantities/Length.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public sealed partial class Length
public partial struct Length
#endif
{
private const double FeetToInches = 12;
private const double InchesInOneFoot = 12;

/// <summary>
/// Converts the length to a customary feet/inches combination.
Expand All @@ -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);
}
}

Expand All @@ -64,7 +64,7 @@ public FeetInches FeetInches
/// </summary>
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
Expand Down
13 changes: 7 additions & 6 deletions UnitsNet/CustomCode/Quantities/Mass.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
/// </summary>
private const double StoneToPounds = 14;
private const double StonesInOnePound = 14.0;

/// <summary>
/// Converts the mass to a customary stone/pounds combination.
Expand All @@ -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);
}
}

Expand All @@ -72,7 +73,7 @@ public StonePounds StonePounds
/// </summary>
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
Expand Down