1

I am using following function to convert a double(mostly int) to string, it is doing good for values <1000000, but for >1000000 it is giving answers in exponential 1e+06 i want it to show full string instead of converting it to exponentials. ** I am using c++ <2008 so please post relevant answers for the same**

#include <iostream>
#include <sstream>

using namespace std;
std::string toString(double C)
    {
      stringstream  tmp;
      tmp << C;
      return tmp.str();
    }

int main()
{
    cout<<toString(1000000)<<endl;
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
wheezay
  • 101
  • 7
  • Next question will likely be how do I get rid of the decimal point and trailing zeroes. I'll see if I can dig up an existing answer for that as well – user4581301 Nov 16 '20 at 04:54
  • 5
    @ouchane.exe That doesn't work, I'm afraid. – user4581301 Nov 16 '20 at 04:55
  • 1
    I'm not finding a good one for reining in the precision, so `tmp << std::fixed << std::setprecision(0) << C;` – user4581301 Nov 16 '20 at 04:59
  • it is not about cout and scientific notation, I am using this function to convert the double value to string and then save it to xml, i think the function toString is returning value as exponential and i want all the numbers not exponential. cout is only for showing a reproducible example – wheezay Nov 16 '20 at 05:14
  • 1
    `cout` and `tmp` are streams. Everything you can do to `cout` you can do to `tmp`. The difference here is `cout` writes to the console and `tmp` writes to a buffer you can transform into a `std::string`. – user4581301 Nov 16 '20 at 05:33
  • 1
    [Demonstration](https://ideone.com/xzYKx8) – user4581301 Nov 16 '20 at 05:36
  • thanks a ton @user4581301 i understand it now. – wheezay Nov 16 '20 at 05:42

0 Answers0