mhm
mhm

Reputation: 313

How to prevent scientific notation from a string generated from "number to string" function?

I have a number_to_string function which converts numbers to string. Now I want to output the resulted string without scientific notation. Here's my code :

#include <iostream>
#include <sstream>
using namespace std;

string number_to_string( double value )
{
    string result;
    stringstream convert;

    convert << value;
    result = convert.str();

    return result;
}

int main()
{
    double d = 591284081248903124;
    string s = number_to_string( d );
    cout << s << endl; // why it shows 5.91284e+17 ?!?!

    return 0;
}

Upvotes: 1

Views: 316

Answers (1)

Alex Telishev
Alex Telishev

Reputation: 2274

Add convert << std::setiosflags (std::ios::fixed); before line convert << value;. And don't forget to #include <iomanip>.

Upvotes: 2

Related Questions