I've got a double containing a number of seconds, which may be negative, and I'd like a string in the format H:mm:ss.hhh or -H:mm:ss.hhh
std::string getFormattedTime(double seconds)
{
// magic voodoo
}
I'm going to need to omit the hour if it's zero.
I've buggered it up twice with various rounding and precision problems, so I figured it was time to ask for help :)
std::string getLabelForPosition(double seconds)
{
bool negative = seconds < 0.0;
if (negative)
seconds *= -1.0;
double mins = std::floor(std::round(seconds) / 60.0);
double secs = seconds - mins * 60.0;
std::stringstream s;
if (negative)
s << "-";
s << mins << ":" << std::fixed << std::setprecision(decimalPlaces) << secs;
return s.str();
}