Function not returing specified precision for a double value?

Revision en1, by mohitm, 2020-01-31 18:48:37

Below is the code for a function returing a double value as string whose precision is 15(i.e. no. of digit after decimal should be 15)

Click here to see the execution on ide

#include <bits/stdc++.h>
using namespace std;

string doit(){

    cout<<setprecision(15)<<fixed;

    double val = 4444/21;

    // want to return it as string
    string ans = to_string(val);

    return ans;
}

int main() {

    string ans = doit();
    cout<<ans;
    return 0;
}

output is

211.000000

I want the output to contain 15 digits after decimal and it should be returned by the function and not use print/cout statement inside the function.

Tags c++ 14, precision, c++, decimal

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English mohitm 2020-01-31 18:48:37 883 Initial revision (published)