c++ - 将持续时间值设为双倍

标签 c++ duration chrono

假设,我有以下持续时间值:

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

我想知道这是多少小时(作为 double 值)。

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

最佳答案

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();

或者,简单地将基于整数的持续时间除以一个基于 double 的 hours:

cout << (12h+15min+99s+99ms)/1.0h << '\n';

关于c++ - 将持续时间值设为双倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60782861/

相关文章:

c++ - opengl 3d 空间平面上的程序网格

Java 将计算列添加到 CSV 文件

c++ - 关于c++ lambda的问题(具有非自动存储持续时间)

mysql - PhpStorm:MySQL 执行时间格式

c++ - std::chrono::from_stream 可以以微秒精度将字符串转换为 time_point 吗?

rust - 使用 chrono 处理 rust 中的 unix 时间戳

c++ - 多态性和数据隐藏 : Does a base class override or ignore a derived class' access restrictions?

c++ - Boost Overload 奇怪的行为.. `int (int ), int (std::string )` 与 `int (int ), int (std::string ), std::string (std::string)` 有何不同?

c++ - 如何使用模板模板参数来专门化类模板?

c++ - chrono::month 和 chrono::months 有什么区别