File tree 4 files changed +46
-12
lines changed
UnitsNet/CustomCode/Quantities
UnitsNet.Tests/CustomCode 4 files changed +46
-12
lines changed Original file line number Diff line number Diff line change @@ -153,5 +153,21 @@ public void MinValueIsCorrectForUnitWithBaseTypeDouble()
153
153
{
154
154
Assert . Equal ( double . MinValue , Length . MinValue . Meters ) ;
155
155
}
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
+ }
156
172
}
157
173
}
Original file line number Diff line number Diff line change 20
20
// THE SOFTWARE.
21
21
22
22
using System ;
23
+ using UnitsNet . Units ;
23
24
using Xunit ;
24
25
25
26
namespace UnitsNet . Tests . CustomCode
@@ -106,5 +107,21 @@ public void MassTimesAccelerationEqualsForce()
106
107
Force force = Mass . FromKilograms ( 18 ) * Acceleration . FromMetersPerSecondSquared ( 3 ) ;
107
108
Assert . Equal ( force , Force . FromNewtons ( 54 ) ) ;
108
109
}
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
+ }
109
126
}
110
127
}
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ public sealed partial class Length
42
42
public partial struct Length
43
43
#endif
44
44
{
45
- private const double FeetToInches = 12 ;
45
+ private const double InchesInOneFoot = 12 ;
46
46
47
47
/// <summary>
48
48
/// Converts the length to a customary feet/inches combination.
@@ -51,11 +51,11 @@ public FeetInches FeetInches
51
51
{
52
52
get
53
53
{
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 ;
57
57
58
- return new FeetInches ( wholeFeet , inches ) ;
58
+ return new FeetInches ( feet , inches ) ;
59
59
}
60
60
}
61
61
@@ -64,7 +64,7 @@ public FeetInches FeetInches
64
64
/// </summary>
65
65
public static Length FromFeetInches ( double feet , double inches )
66
66
{
67
- return FromInches ( FeetToInches * feet + inches ) ;
67
+ return FromInches ( InchesInOneFoot * feet + inches ) ;
68
68
}
69
69
70
70
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ public static Mass FromGravitationalForce(Force f)
50
50
/// StonePounds related code makes it easier to work with Stone/Pound combination, which are customarily used in the UK
51
51
/// to express body weight. For example, someone weighs 11 stone 4 pounds (about 72 kilograms).
52
52
/// </summary>
53
- private const double StoneToPounds = 14 ;
53
+ private const double StonesInOnePound = 14.0 ;
54
54
55
55
/// <summary>
56
56
/// Converts the mass to a customary stone/pounds combination.
@@ -59,11 +59,12 @@ public StonePounds StonePounds
59
59
{
60
60
get
61
61
{
62
- double totalPounds = Pounds ;
63
- double wholeStone = Math . Floor ( totalPounds / StoneToPounds ) ;
64
- double pounds = totalPounds % StoneToPounds ;
62
+ var inPounds = Pounds ;
65
63
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 ) ;
67
68
}
68
69
}
69
70
@@ -72,7 +73,7 @@ public StonePounds StonePounds
72
73
/// </summary>
73
74
public static Mass FromStonePounds ( double stone , double pounds )
74
75
{
75
- return FromPounds ( StoneToPounds * stone + pounds ) ;
76
+ return FromPounds ( StonesInOnePound * stone + pounds ) ;
76
77
}
77
78
78
79
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
You can’t perform that action at this time.
0 commit comments