Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string decimalToFraction(double number) {
- double intVal = floor(number);
- double fVal = number - intVal;
- int pVal = 1e9;
- int gcdVal = __gcd((int)round(fVal * pVal), pVal);
- int num = round(fVal * pVal) / gcdVal;
- int deno = pVal / gcdVal;
- num = (intVal * deno) + num;
- //cerr << num << "/" << deno << endl;
- string ans;
- ans += to_string(num);
- ans += '/';
- ans += to_string(deno);
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement