5 Replies - 4985 Views - Last Post: 17 May 2011 - 08:50 AM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

string stream and double precision

Posted 17 May 2011 - 07:52 AM

Hi everybody,
Does anyone know how to put a double into a stringstream and maintain its precision when it has .0 for a decimal? At the moment, if I run code like this:

#include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main() {
stringstream test( stringstream::in | stringstream::out );
double testDouble = 3.0;
string testString = 3.0;
test << testDouble;
cout << testString << "\t" << testDouble << endl;
return 0;
}



I get this for output:
3.0    3


When I expect this:
3.0    3.0


Can anyone tell me how to keep this precision?

Thanks,
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: string stream and double precision

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,681
  • Joined: 25-December 09

Re: string stream and double precision

Posted 17 May 2011 - 07:57 AM

You need to use the stream.showpoint() from the iomanip header. Also maybe ios::fixed.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: string stream and double precision

Posted 17 May 2011 - 08:10 AM

Thank you jimblumberg,
That doesn't seem to work for me. If I add either fixed or showpoint to where the double is added to the stream, I get this:

3.0    3.000000


So the decimal points still aren't the same. I think I'm still missing something, do you know what it could be?

Thanks for your help so far,
Zel2008

This post has been edited by Zel2008: 17 May 2011 - 08:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: string stream and double precision

Posted 17 May 2011 - 08:20 AM

Try:
	cout.precision(1);
	cout << testString << "\t" << testDouble << endl;


Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: string stream and double precision

Posted 17 May 2011 - 08:26 AM

3 == 3.0 == 3.000 == 3.0000 == 3.000000000000000000000000

SO it is up to you to tell the program how you would like the number formatted. Using showpoint tells the program to show the decimal point when it is not needed (i.e. it would show 3.14 but 3.0 == 3 which is simpler). So now you need to tell it the precision - i.e. the number of decimal places to use.

3.0 uses 2 decimal places therefore:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

int main() {
    string data = "3.0";
    double asDouble;
    
    istringstream toDouble(data);
    toDouble >> asDouble;
    
    cout << showpoint << setprecision(2)  << asDouble << endl;    
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: string stream and double precision

Posted 17 May 2011 - 08:50 AM

Thank you all, I have it working now.
Zel2008
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1