Advertisement
STANAANDREY

decimalToFraction

Jan 8th, 2021
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. string decimalToFraction(double number) {
  2.     double intVal = floor(number);
  3.     double fVal = number - intVal;
  4.     int pVal = 1e9;
  5.     int gcdVal = __gcd((int)round(fVal * pVal), pVal);
  6.     int num = round(fVal * pVal) / gcdVal;
  7.     int deno = pVal / gcdVal;
  8.     num = (intVal * deno) + num;
  9.     //cerr << num << "/" << deno << endl;
  10.     string ans;
  11.     ans += to_string(num);
  12.     ans += '/';
  13.     ans += to_string(deno);
  14.     return ans;
  15. }
  16.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement