Question

I have a function that converts a decimal to its fraction equivalent. I have found some of the fractions but i cannot seem to find the sixteenths (1/16) fractions. Anyone got any suggestions as to where i can look on the net or an way for me to convert these three character fractions to a one character fraction.

I have a bitmap below that shows where the 16nths fractions take up so much realestate, unlike the other fractions which look nice and small.

I've added the function that I am using to convert these fractions below.

I've checked everywhere online and haven't got this resolved yet.

If i could get something like here http://msdn.microsoft.com/en-us/library/ms745109.aspx about three quarters of the way down the page you'll see the Numerical Types. If i could get something like the stacked version for the windows graphics that would be a win.

Door Details

static string getFractionFromDecimal(decimal dec)
    {
        string d = dec.ToString(), dd = string.Empty;
        switch (d)
        {
            case "0.0625": dd = " 1/16"; break;
            case "0.1250": dd = "⅛"; break;
            case "0.1875": dd = " 3/16"; break;
            case "0.2500": dd = "¼"; break;
            case "0.3125": dd = " 5/16"; break;
            case "0.3750": dd = "⅜"; break;
            case "0.4375": dd = " 7/16"; break;
            case "0.5000": dd = "½"; break;
            case "0.5625": dd = " 9/16"; break;
            case "0.6250": dd = "⅝"; break;
            case "0.6875": dd = " 11/16"; break;
            case "0.7500": dd = "¾"; break;
            case "0.8125": dd = " 13/16"; break;
            case "0.8750": dd = "⅞"; break;
            case "0.9375": dd = " 15/16"; break;
        }; return dd;
    }
Was it helpful?

Solution

I've written a small class for you which could help you to draw a fraction. Please note that I've hardcoded many values, that's not good practice. If you're going to use this concept, I suggest you to parametrize the multipliers inside of the code.

public class FractionLabel : Control
{
    public int Numerator { get; set; }
    public int Denominator { get; set; }
    public Brush Brush { get; set; }

    protected override void OnPaint(PaintEventArgs e)
    {
        var graphics = e.Graphics;
        var font = Font;
        var n = Numerator.ToString();
        var d = Denominator.ToString();
        var numSize = graphics.MeasureString(n, font);
        graphics.DrawString(n, Font, Brush, new PointF());
        //Comment out the following line if you want sharp lines
        graphics.SmoothingMode = SmoothingMode.AntiAlias;
        using (var pen = new Pen(Brush))
        {
            var p1 = new PointF(numSize.Width/2, numSize.Height*5/4);
            var p2 = new PointF(numSize.Width*5/4, numSize.Height/2);
            graphics.DrawLine(pen, p1, p2);
        }
        var dPos = new PointF(numSize.Width*3/4, numSize.Height*3/4);
        graphics.DrawString(d, Font, Brush, dPos);
    }
}

Sample usage:

var label = new FractionLabel
            {
                Numerator = 3,
                Denominator = 16,
                Font = new Font("Arial", 8),
                Brush = Brushes.Black,
                Width = 100,
                Height = 100
            };
Controls.Add(label);

If you modify this class and integrate it, you can modify your method to return a FractionLabel instead of a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top