Get the simple fraction of decimal number

89 Views Asked by At

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

Example:

$1$ would be $\frac11$, $16$ would be $\frac{16}{1}$, $0.125$ would be $\frac{1}{8}$, $0.30769231$ would be $\frac{4}{13}$

I'm going to use this operation in code but i need to know the process to the get the fraction with integer numbers...

1

There are 1 best solutions below

0
On

If this is just a programming problem then you don't have to worry about never ending decimals. In that case, you can get the fraction by taking the number formed by all the digits (on both sides of the decimal point) as the numerator and $10^n$ as the denominator, where $n$ is the number of decimal places. Then simplify the resulting fraction by trying common factors. For example: $$23.70272 = \frac{2370272}{100000}=\frac{74071}{3125}$$