我有一个包含秒数的可能是负数的double,我想要一个格式为H:mm:ss.hhh的字符串或-H:MM:ss.hhh将秒转换为小时,分钟,seconds.hundreths第二秒
std::string getFormattedTime(double seconds)
{
// magic voodoo
}
我会需要省略小时,如果是零。
我已经累垮它两次不同的舍入和精度的问题,所以我想现在是时候寻求帮助:)
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();
}
首先,你需要在函数内部创建一个空白字符串。然后我会参考这个页面来添加int到字符串。 https://stackoverflow.com/questions/45505477/append-int-to-stdstring阅读后,它只是做所有的单位转换,并将它们与适当的标点符号一起附加。 – 2017-08-25 15:48:48