#include #include //for ostringstream #include using namespace std; string formatDouble(long double var) { ostringstream s; s << var; sTmp = string(s); size_t len = sTmp.size() - 1; //should be string::size_type while (sTmp[len] == '0') { sTmp = sTmp.sub_str(0, --len); if (sTmp[len] == '.') { sTmp = sTmp.sub_str(0, --len); break; } } return sTmp.empty() ? "0" : sTmp; } int main() { cout << formatDouble(3.14) << "\n"; return EXIT_SUCCESS; }