0

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...

emil
  • 1,310
Chris
  • 1
  • Is this a math problem or a programming problem? Notice the last example is wrong, 4/13 has a never ending expansion. – Sam Apr 22 '20 at 16:29
  • Programming problem, i need to get the fraction with integer number for decimal values... – Chris Apr 22 '20 at 16:38
  • Using continuous fractions may help – Damien Apr 22 '20 at 16:40
  • See also https://math.stackexchange.com/questions/2540334/approximation-of-rational-numbers and https://math.stackexchange.com/a/3427322/589 – lhf Apr 22 '20 at 17:33

1 Answers1

1

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}$$

Sam
  • 1,381