duration_cast如何进行 [英] How does duration_cast round
问题描述
如果我将时间单位转换为较粗略的时间(例如,将 std :: chrono ::分钟转换为
std :: chrono ::小时
) duration_cast
将如何取整?例如,如果转换为 std :: chrono :: hours $ c,
std :: chrono :: minutes(91)
会变成什么值$ c>? 2h,1h?
If I convert to a coarser unit of time (say std::chrono::minutes
to std::chrono::hours
) how will duration_cast
round? For example, what value will std::chrono::minutes(91)
become if converted to std::chrono::hours
? 2h, 1h?
推荐答案
duration_cast
总是四舍五入。即正值向下取整,负值向上取整。
duration_cast
always rounds towards zero. I.e. positive values round down and negative values round up.
有关其他取整选项,请参见:
For other rounding options see:
http://howardhinnant.github.io/duration_io/chrono_util.html
楼层
,天花板
和回合
当前在C ++ 1z(希望是C ++ 17)草稿中。同时,请随时使用 chrono_util.html 中的代码,请让我知道您是否有任何问题。
floor
, ceil
, and round
are currently in the draft C++ 1z (hopefully C++17) draft working paper. In the meantime feel free to use the code at chrono_util.html, and please let me know if you have any problems with it.
- std::chrono::floor
- std::chrono::ceil
- std::chrono::round
std::chrono::floor<std::chrono::seconds>(1400ms) == 1s
std::chrono::floor<std::chrono::seconds>(1500ms) == 1s
std::chrono::floor<std::chrono::seconds>(1600ms) == 1s
std::chrono::floor<std::chrono::seconds>(-1400ms) == -2s
std::chrono::floor<std::chrono::seconds>(-1500ms) == -2s
std::chrono::floor<std::chrono::seconds>(-1600ms) == -2s
这篇关于duration_cast如何进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!