我有一個包含秒數的可能是負數的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