// 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 = FractionConverter.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; string s2; s1 = d1 == 0 ? "" : d1.ToString() + "'"; s2 = d2 == 0 ? "" : FractionConverter.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); }
public static string Convert(decimal pvalue, bool skip_rounding = false, decimal dplaces = (decimal)0.0625) { decimal value = pvalue; if (!skip_rounding) { value = FractionConverter.DecimalRound(pvalue, dplaces); } if (value == Math.Round(value, 0)) // whole number check { return(value.ToString()); } // get the whole value of the fraction decimal mWhole = Math.Truncate(value); // get the fractional value decimal mFraction = value - mWhole; // initialize a numerator and denomintar uint mNumerator = 0; uint mDenomenator = 1; // ensure that there is actual a fraction if (mFraction > 0m) { // convert the value to a string so that // you can count the number of decimal places there are string strFraction = mFraction.ToString().Remove(0, 2); // store the number of decimal places uint intFractLength = (uint)strFraction.Length; // set the numerator to have the proper amount of zeros mNumerator = (uint)Math.Pow(10, intFractLength); // parse the fraction value to an integer that equals // [fraction value] * 10^[number of decimal places] uint.TryParse(strFraction, out mDenomenator); // get the greatest common divisor for both numbers uint gcd = GreatestCommonDivisor(mDenomenator, mNumerator); // divide the numerator and the denominator by the greatest common divisor mNumerator = mNumerator / gcd; mDenomenator = mDenomenator / gcd; } // create a string builder StringBuilder mBuilder = new StringBuilder(); // add the whole number if it's greater than 0 if (mWhole > 0m) { mBuilder.Append(mWhole); } // add the fraction if it's greater than 0m if (mFraction > 0m) { if (mBuilder.Length > 0) { mBuilder.Append(" "); } mBuilder.Append(mDenomenator); mBuilder.Append("/"); mBuilder.Append(mNumerator); } return(mBuilder.ToString()); }