Succinct way to round to fractional inches

Math.Round(n*r*12,0)/(r*12); with r being the fraction of an inch desired.


PS: Throw in this to match Revit’s default feet/inch format:

1 Like

You could also try this …
decimalToFraction

a = Math.Floor(n);
b = (n - a) * 12;
c = Math.Floor(b);
(b - c)  <= 0.0625 && r >= 3? a + "'-" + c + " 1/16" :
(b - c) <= 0.125 && r >= 2 ? a + "'-" + c + " 1/8" :
(b - c) <= 0.1875 && r >= 3 ? a + "'-" + c + " 3/16" :
(b - c) <= 0.25 && r >= 1 ? a + "'-" + c + " 1/4" :
(b - c) <= 0.3125 && r >= 3 ? a + "'-" + c + " 5/16" :
(b - c) <= 0.375 && r >= 2 ? a + "'-" + c +  " 3/8" :
(b - c) <= 0.4375 && r >= 3 ? a + "'-" + c +  " 7/16" :
b - c <= 0.5 ? a + "'-" + c +  " 1/2" :
(b - c) <= 0.5625 && r >= 3 ? a + "'-" + c +  " 9/16" :
(b - c) <= 0.625 && r >= 2 ? a + "'-" + c +  " 5/8" :
(b - c) <= 0.6875 && r >= 3 ? a + "'-" + c +  " 11/16" :
(b - c) <= 0.75 && r >= 1 ? a + "'-" + c +  " 3/4" :
(b - c) <= 0.8125 && r >= 3 ? a + "'-" + c +  " 13/16" :
(b - c) <= 0.875 && r >= 2 ? a + "'-" + c +  " 7/8" :
(b - c) < 1 && r >= 3 ? a + "'-" + c + " 15/16" :
a + "'-" + c;

decimalToFraction.dyn (8.4 KB)

6 Likes

Why doesn’t it round up?