Search code examples
c#mathfractions

Get the simple fraction of decimal number c#


I need to get the simple fraction of a decimal number in c#

Example:

1 would be 1 /1
16 would be 16 / 1
0.125 would be 1 / 8
.30769231 would be 4/13

The solution i found is this:

Decimal_target= 0.1

In the numerator and denominator for the decimal values i used (Decimal_target*(random number between 1- 10)/random number)

The random number got was 9 so: ((0.1*9)/9)=0.1

Numerator = 0.9
Denominator = 9

But the numerator and denominator needs to be integer numbers...


Solution

  • Here is sample code. Can get stuck in infinite loop (let OP solve this issue). I rounded to number of decimal places in input :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] numbers = { "1", "16", "0.125", ".30769231" };
    
                foreach (string number in numbers)
                {
                    int numerator = 1;
                    string strNumerator = "";
                    int denominator = 1;
                    string strDenominator = "";
    
    
                    Boolean done = false;
    
                    if (!number.Contains("."))
                    {
                        strNumerator = number;
                        strDenominator = "1";
                        done = true;
                    }
                    else
                    {
                        int numberDecimalPlaces = number.Substring(number.IndexOf(".")).Length - 1;
                        string tempStr = number;
                        if (number.StartsWith(".")) tempStr = "0" + tempStr;
                        double value = double.Parse(number);
    
                        while(!done)
                        {
                            while (true)
                            {
                                double newTemp = numerator / (double)denominator;
                                string newTempStr = newTemp.ToString("F" + numberDecimalPlaces.ToString());
                                if (newTempStr == tempStr)
                                {
                                    strNumerator = numerator.ToString();
                                    strDenominator = denominator.ToString();
                                    done = true;
                                    break;
                                }
                                denominator++;
                                if (newTemp <= value)
                                {
                                    numerator += 1;
                                    denominator = numerator + 1;
                                    break;
                                }
    
                            }
    
                        }
    
                    }
                    Console.WriteLine("Results for : '{0}', numerator = '{1}', denominator = '{2}'", number, strNumerator, strDenominator);
                }
                Console.ReadLine();
            }
        }
    
    }