将持续时间值设为双倍

Getting duration value as double

比方说,我有以下持续时间值:

auto duration=12h+15min+99s+99ms;

我想知道那是多少小时(双精度值)。

当我执行 auto hours=std::chrono::duration_cast<std::chrono::hours>(duration) 时,我得到 hours.count()int。将整个持续时间的值表示为双精度的正确方法是什么?

using namespace std::chrono;

// Create a double-based hours duration unit
using dhours = duration<double, hours::period>;

// Assign your integral-based duration to it
dhours h = 12h+15min+99s+99ms;

// Get the value
cout << h.count();

或者,只需将基于积分的持续时间除以一个基于双倍的 hours:

cout << (12h+15min+99s+99ms)/1.0h << '\n';
#include<iostream>
#include<chrono>
using namespace std::chrono_literals;

int main() {


    auto duration = 12.0h+15min+99s+99ms;
    std::cout << std::chrono::duration_cast<std::chrono::duration<long double,std::ratio<3600,1>>>(duration).count();

}