I have a function that is supposed to return a formatted string (hh:mm:sec.xxx)
I want the output to display 2 places for hours, 2 places for minutes, and 5 places for seconds (truncated microseconds);
My code:
#include <iostream>
#include <iomanip>
std::string getTime(uint64_t time){ //input is in microseconds
unit64_t tempTime = time;
unit64_t hours = (tempTime / 3600000000);
tempTime -= (hours * 3600000000);
unit64_t mins = (tempTime / 60000000);
tempTime -= (mins * 60000000);
double secs = ((double)tempTime / 1000000);
std::string strTime;
stringstream ss;
ss << std::setprecision(2)<<hours<<":"
<< std::setprecision(2)<<mins <<":"
<< std::setprecision(2)<<std::setprecision(2)<<secs;
strTime = ss.str();
return strTime;
}
could somebody please show me the proper syntax for inputting the values to ss. It compiles and works but I do not get the format I want.
I just now typed this up for this question so if you see typos please forgive me
You forgot about minutes and you probably want 0 decimal precision. Try: