// Rounds a number to the nearest decimal. // For instance, carpenters do not want to see a number like 4/5. // That means nothing to them // and you'll have an angry carpenter on your hands // if you ask them cut a 2x4 to 36 and 4/5 inches. // So, we would want to convert to the nearest 1/16 of an inch. // Example: DecimalRound(0.8, 0.0625) Rounds 4/5 to 13/16 or 0.8125. private static decimal DecimalRound(decimal val, decimal places) { string sPlaces = MyFractions.Convert(places, true); string[] s = sPlaces.Split('/'); if (s.Count() == 2) { int nPlaces = System.Convert.ToInt32(s[1]); decimal d = Math.Round(val * nPlaces); return(d / nPlaces); } return(val); }
// Converts a value to feet and inches. // Examples: // 12.1667 converts to 12' 2" // 4 converts to 4' // 0.1667 converts to 2" public static bool ReformatForFeetAndInches(ref string line_type, bool zero_for_blank = true) { if (string.IsNullOrEmpty(line_type)) { if (zero_for_blank) { line_type = "0'"; } return(true); } decimal d = System.Convert.ToDecimal(line_type); decimal d1 = Math.Floor(d); decimal d2 = d - d1; d2 = Math.Round(d2 * 12, 2); string s1 = null; string s2 = null; s1 = d1 == 0 ? "" : d1.ToString() + "'"; s2 = d2 == 0 ? "" : MyFractions.Convert(d2) + "\""; line_type = string.Format("{0} {1}", s1, s2).Trim(); if (string.IsNullOrEmpty(line_type)) { if (zero_for_blank) { line_type = "0'"; } return(true); } return(true); }