Created
March 23, 2017 15:50
-
-
Save alexcmd/6f699389862f912959d8a6c2d0e187bd to your computer and use it in GitHub Desktop.
Converter floating point number to Fraction representation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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