web analytics
Conveting double to string using stringstream in C++ Options
davegate
Posted: Wednesday, January 27, 2016 1:00:34 PM

Rank:Advanced Member
Groups: Member
Joined: 12/14/2015
Posts: 182
Points: 894

The follwing C++ code demonstrates how to convert a double to string using stringstream. We also use two manipulators setiosflags and setprecision to ensure the code behave correctly.

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

void main()
{
 double d = 1600564.00;

 stringstream ss;
 ss << setiosflags(ios::fixed) << setprecision(2) << d;

 string str = ss.str();

 ss.clear();

}
Sponsor
Posted: Wednesday, January 27, 2016 1:00:34 PM
 
davegate
Posted: Wednesday, January 27, 2016 1:03:21 PM

Rank:Advanced Member
Groups: Member
Joined: 12/14/2015
Posts: 182
Points: 894

The following C++ code can do the same thing.

 //using sprintf
 char str2[11] = "\0";
 sprintf(str2, "%.2f", d);
Users browsing this topic
Guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2018 Digcode.com. All rights reserved.