The Code is as follows:
Copy codeThe Code is as follows: # include <stdlib. h>
# Include <string>
# Include <windows. h>
# Include <stdio. h>
# Include <iostream>
# Include <limits>
# Include <sstream>
Using namespace std;
String do_fraction (long double val, int decplaces = 3)
{
Ostringstream out;
Char DECIMAL_POINT = '.'; // European format ','
Int prec = numeric_limits <long double>: digits10; // 18
Out. precision (prec); // overwrite the default precision
Out <val;
String str = out. str (); // extract the string from the stream
Size_t n = str. find (DECIMAL_POINT );
If (n! = String: npos) // do you have a decimal point?
& (Str. size ()> n + decplaces) // is there at least a decplaces bit behind it?
{
Str [n + decplaces] = '\ 0'; // overwrite the first redundant number.
}
Str. swap (string (str. c_str (); // Delete extra characters After nul
Return str;
}