Skip to content
Open
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
21 changes: 19 additions & 2 deletions UnitsNet/CustomCode/Quantities/Length.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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);

Expand All @@ -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)
Expand Down Expand Up @@ -238,7 +255,7 @@ int GreatestCommonDivisor(int a, int b)
return inchPart.ToString();
}

return $"{Feet}' - {inchPart}";
return $"{feet}' - {inchPart}";
}
}
}
Loading