From 34c1caeb39dfbf958fb8ec1a123812c408703fc2 Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Tue, 21 Apr 2026 14:11:40 -0400 Subject: [PATCH 1/2] add handling for FeetInches output where rounding makes inches =12 --- UnitsNet/CustomCode/Quantities/Length.extra.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/UnitsNet/CustomCode/Quantities/Length.extra.cs b/UnitsNet/CustomCode/Quantities/Length.extra.cs index 138d9e9f7f..54a3df7286 100644 --- a/UnitsNet/CustomCode/Quantities/Length.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Length.extra.cs @@ -161,7 +161,17 @@ public string ToString(IFormatProvider? cultureInfo) // Note that it isn't customary to use fractions - one wouldn't say "I am 5 feet and 4.5 inches". // So inches are rounded when converting from base units to feet/inches. - return string.Format(cultureInfo, "{0:n0} {1} {2:n0} {3}", Feet, footUnit, Math.Round(Inches), inchUnit); + // When we do this we check if we rounded inches to 12(InchesInOneFoot). + // If it does feet/inches are fixed something like 4 ft 0 in is displayed instead of 3ft 12 in for things very close to 4 e.g. 3.9999 ft + var feet = Feet; + var inches = Math.Round(Inches); + if(inches == InchesInOneFoot) + { + feet++; + inches = 0; + } + + return string.Format(cultureInfo, "{0:n0} {1} {2:n0} {3}", feet, footUnit, inches, inchUnit); } /// From 7dbf55c82b55afbdb9e28aee5826142fd6a42f42 Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Tue, 21 Apr 2026 14:31:16 -0400 Subject: [PATCH 2/2] update ToArchitecturalString to not output 12 in the inches position --- UnitsNet/CustomCode/Quantities/Length.extra.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/UnitsNet/CustomCode/Quantities/Length.extra.cs b/UnitsNet/CustomCode/Quantities/Length.extra.cs index 54a3df7286..d2a05b0fac 100644 --- a/UnitsNet/CustomCode/Quantities/Length.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Length.extra.cs @@ -200,6 +200,7 @@ public string ToArchitecturalString(int fractionDenominator) throw new ArgumentOutOfRangeException(nameof(fractionDenominator), "Denominator for fractional inch must be greater than zero."); } + var feet = Feet; var inchTrunc = (int)Math.Truncate(Inches); var numerator = (int)Math.Round((Inches - inchTrunc) * fractionDenominator); @@ -209,6 +210,12 @@ public string ToArchitecturalString(int fractionDenominator) numerator = 0; } + if (inchTrunc == InchesInOneFoot) + { + feet++; + inchTrunc = 0; + } + var inchPart = new System.Text.StringBuilder(); if (inchTrunc != 0 || numerator == 0) @@ -248,7 +255,7 @@ int GreatestCommonDivisor(int a, int b) return inchPart.ToString(); } - return $"{Feet}' - {inchPart}"; + return $"{feet}' - {inchPart}"; } } }