I have a specific case where I need to return 'Vulgar' fractions where ever possible. The code that I have come up with is as follows
var complexFractionString = heaveValue.ToComplexFractionString();
complexFractionString = complexFractionString.Replace("1/8", "⅛");
complexFractionString = complexFractionString.Replace("1/4", "¼");
complexFractionString = complexFractionString.Replace("3/8", "⅜");
complexFractionString = complexFractionString.Replace("1/2", "½");
complexFractionString = complexFractionString.Replace("5/8", "⅝");
complexFractionString = complexFractionString.Replace("3/4", "¾");
complexFractionString = complexFractionString.Replace("7/8", "⅞");
return complexFractionString;
.ToComplexFractionString() is a method that returns the least common denominator fraction of the double in string format. i.e. an input of .5 would return "1/2"
This code works for my use case right now but I do not like how it is structured at all. It is very brittle in that a fraction of "1/16" or "1/32" would get through without being changed and it the code is a bunch of lines to do something that should be relatively easy.
Is there a better way to do this in C#?