I haven’t posted much code to my blog lately, so I thought I’d pass along some general-purpose C# code that I recently used in a project. I’m working on a system right now where the original author made some, shall we say, interesting decisions about how to store data in SQL. Specifically, he used varchar fields for most of the numeric data. And, in those fields, he sometimes stores the data in decimal format (e.g. “1.5”), sometimes as fractions (e.g. “1 1/2”) and sometimes as explicitly-signed fractions (“+1 1/2”). I, of course, need to do LOTS of math on these numbers. The decimal fields can be dealt with using good old TryParse and ToString of course, but there’s no obvious parse routine for fractions, nor is there an obvious way to turn a decimal number back into a fraction string.

The internet, of course, provides. Here is a VB.NET function to turn a fraction string into a decimal and there is some C# code to convert a decimal into a fraction string in this thread. I converted the VB.NET to C#, and cleaned both of them up and put them in a utility class. Here it is:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; namespace MySuit.MySuitV2.BLL { public class Utility { public static decimal FractionToDecimal(string frac) { // this method should convert a fraction, e.g. "12 1/4" to a decimal, e.g. 12.25. // based on http://amrelgarhytech.blogspot.com/2008/03/fraction-to-decimal.html // TODO: not sure how best to handle exceptions here. (parse errors, div by zero, null/empty string input...) decimal rv; int numerator, denominator, wholePart = 0; int sign = 1; if (string.IsNullOrEmpty(frac)) return 0m; // deal with signs frac = frac.Trim().TrimStart('+'); if (frac[0] == '-') { frac = frac.TrimStart('-'); sign = -1; } frac = frac.Trim(); if (frac.IndexOf("/") > 0) { if (frac.IndexOf(" ") > 0) { wholePart = int.Parse(frac.Substring(0, frac.IndexOf(" "))); frac = frac.Substring(frac.IndexOf(" ")); } numerator = int.Parse(frac.Substring(0, frac.IndexOf("/"))); denominator = int.Parse(frac.Substring(frac.IndexOf("/") + 1)); rv = sign * (wholePart + ((decimal)numerator / denominator)); } else { rv = decimal.Parse(frac); } return rv; } public static string DecimalToFractionSigned(decimal value) { // always put a sign (+/-) in front string rv = DecimalToFraction(value); if (rv[0] != '-') rv = string.Format("+{0}", rv); return rv; } public static string DecimalToFraction(decimal value) { // taken from here: http://bit.ly/tHaKrK and modified to work with negative numbers too. int sign = 1; if (value < 0) { value = Math.Abs(value); sign = -1; } // get the whole value of the fraction decimal mWhole = Math.Truncate(value); // get the fractional value decimal mFraction = value - mWhole; // initialize a numerator and denominator uint mNumerator = 0; uint mDenominator = 1; // ensure that there is actually 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 mDenominator); // get the greatest common divisor for both numbers uint gcd = GreatestCommonDivisor(mDenominator, mNumerator); // divide the numerator and the denominator by the greatest common divisor mNumerator = mNumerator / gcd; mDenominator = mDenominator / 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(mDenominator); mBuilder.Append("/"); mBuilder.Append(mNumerator); } if (sign == -1) mBuilder.Insert(0, '-'); return mBuilder.ToString(); } private static uint GreatestCommonDivisor(uint valA, uint valB) { // return 0 if both values are 0 (no GSD) if (valA == 0 && valB == 0) { return 0; } // return value b if only a == 0 else if (valA == 0 && valB != 0) { return valB; } // return value a if only b == 0 else if (valA != 0 && valB == 0) { return valA; } // actually find the GSD else { uint first = valA; uint second = valB; while (first != second) { if (first > second) { first = first - second; } else { second = second - first; } } return first; } } } }

(This is also in a Gist.)

I hope this helps anyone who might be looking for something similar. Also, I want to reiterate that I didn’t write this code from scratch. I took two existing functions, one in VB and one in C#, converted the VB to C#, cleaned them both up a bit, and put them together.

Luckily, by the way, all of the fractions I’m dealing with resolve to simple decimal numbers; everything is x/2, x/4, or x/8. I don’t have to deal with converting 1/3 to decimal and back. If you need to do that, you probably want a class that stores the fractions as numerator and denominator, and does math on them, as fractions. There are a couple of those out there, if you look around.