diff --git a/UnitsNet/CustomCode/Quantities/Length.extra.cs b/UnitsNet/CustomCode/Quantities/Length.extra.cs index 138d9e9f7f..d2a05b0fac 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); } /// @@ -190,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); @@ -199,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) @@ -238,7 +255,7 @@ int GreatestCommonDivisor(int a, int b) return inchPart.ToString(); } - return $"{Feet}' - {inchPart}"; + return $"{feet}' - {inchPart}"; } } }