Convert percentage (Double) to Fraction using C# in ASP.Net

Last Reply 20 days ago By dharmendr

Posted 21 days ago

hello,

i am using Convert percentage to Fraction using C# and VB.Net in ASP.Net

it works fine in some case but some cases it fails like

33.33% fraction should be 1/3 but it is showing 833/2500

16.67% fraction should be 1/6 but it is showing 1667/10000

please advice

You are viewing reply posted by: dharmendr 20 days ago.
Posted 20 days ago

Hi nauna,

Check this example. Now please take its reference and correct your code.

HTML

<asp:TextBox runat="server" ID="txtPercentage" AutoPostBack="true" OnTextChanged="ConvertToFraction" />&nbsp;%

Code

protected void ConvertToFraction(object sender, EventArgs e)
{
    if (IsDouble(txtPercentage.Text.Trim()))
    {
        double percentage = Convert.ToDouble(txtPercentage.Text);
        double convertToDecimal = percentage / 100;

        Fraction fraction = DoubleToFraction(convertToDecimal);
        string message = "Fraction of " + percentage.ToString() + " is " + fraction.N + "/" + fraction.D;
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('" + message + "');", true);
    }
}

public bool IsDouble(string text)
{
    double number;
    return double.TryParse(text, out number);
}

public Fraction DoubleToFraction(double value)
{
    double accuracy = 0.1;
    if (accuracy <= 0.0 || accuracy >= 1.0)
    {
        throw new ArgumentOutOfRangeException("accuracy", "Must be > 0 and < 1.");
    }

    int sign = Math.Sign(value);

    if (sign == -1)
    {
        value = Math.Abs(value);
    }

    // Accuracy is the maximum relative error; convert to absolute maxError
    double maxError = sign == 0 ? accuracy : value * accuracy;

    int n = (int)Math.Floor(value);
    value -= n;

    if (value < maxError)
    {
        return new Fraction(sign * n, 1);
    }

    if (1 - maxError < value)
    {
        return new Fraction(sign * (n + 1), 1);
    }

    // The lower fraction is 0/1
    int lower_n = 0;
    int lower_d = 1;

    // The upper fraction is 1/1
    int upper_n = 1;
    int upper_d = 1;

    while (true)
    {
        // The middle fraction is (lower_n + upper_n) / (lower_d + upper_d)
        int middle_n = lower_n + upper_n;
        int middle_d = lower_d + upper_d;

        if (middle_d * (value + maxError) < middle_n)
        {
            // real + error < middle : middle is our new upper
            upper_n = middle_n;
            upper_d = middle_d;
        }
        else if (middle_n < (value - maxError) * middle_d)
        {
            // middle < real - error : middle is our new lower
            lower_n = middle_n;
            lower_d = middle_d;
        }
        else
        {
            // Middle is our best fraction
            return new Fraction((n * middle_d + middle_n) * sign, middle_d);
        }
    }
}

public class Fraction
{
    public Fraction(int n, int d)
    {
        N = n;
        D = d;
    }

    public int N { get; set; }
    public int D { get; set; }
}

Screenshot