Skip to content

Instantly share code, notes, and snippets.

@alexcmd
Created March 23, 2017 15:50
Show Gist options
  • Save alexcmd/6f699389862f912959d8a6c2d0e187bd to your computer and use it in GitHub Desktop.
Save alexcmd/6f699389862f912959d8a6c2d0e187bd to your computer and use it in GitHub Desktop.
Converter floating point number to Fraction representation
class Program
{
static void Main(string[] args)
{
double num = 0.3333;
double denom = 1.0 / (Eps * 10.0);
num = (int)(num * denom);
double gcd = (double)find_gcd((int)num, (int)denom);
num /= gcd;
denom /= gcd;
Debug.WriteLine($"{num}/{denom}");
}
static int find_gcd(int a, int b)
{
while (b>0)
{
var t = b;
b = a % b;
a = t;
}
return a;
}
const double Eps = 0.01;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment