Hi all,

I have this string "May 19, 2009 15:39:41.606809000" , i need to get the seconds into a double variable.
My code is like this:

double sec;
string seconds = str.substr(19,10);
istringstream(seconds) >> sec;

The thing is that seconds = 41.6068090 , but sec = 41.6068

Why doesn't the number of digits is lower?

Any guess?
Thanx in advance

Recommended Answers

All 4 Replies

it may be the display of the value, not the actual value itself that's the problem. Try adjusting the display with ostream modifiers like setprecision, setwidth, etc.

Thanks, now it displays correctly!
But, i save this values in a file:

double sec;
               double secundo;

                seconds = str.substr(19,10);
		istringstream(seconds) >> sec;
		cout.precision(10);
		cout.setf(ios::fixed, ios::floatfield);
		cout << "sec: " << sec << endl;
		
		secundo = ((min*60)+sec);
		cout << secundo<< endl;
		frames[i].timesec = secundo;
}

myfile.open ("Exemplo.csv", ios::out | ios::app);
	myfile << "Frame.time_delta" << ";" << "Frame.timesec" << ";" << "Frame.lenght" << ";" << endl;
	
	for(i=1; i<= k ; i++)
	{ 
		myfile << frames[i].time_delta << ";" << frames[i].timesec << ";" << frames[i].lenght << endl;
	}

Secundo is also displaying right, but when i save into a file the value is displayed without the .precision(10), how do i instruct to save with the same precision?

myfile.precision(10); should do the trick I think, filestreams are very similar to cout and cin.

Thanks, it works!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.