Add minutes and seconds. Display in hours

会有一股神秘感。 提交于 2019-12-10 22:53:14

问题


Let's suppose I'd like to add 29 minutes and 60 seconds and display the result in hours.

Here's something that appears to work:

cout <<
    static_cast<quantity<hour_base_unit::unit_type>>
    (quantity<time>{29.0 * minute_base_unit::unit_type()} + 60.0 * seconds)
    << endl;    

The following is displayed on the console:

0.5 h

Is this the recommended approach? Is there a better or more idiomatic way?

The entire program illustrating the above example is below.

#include <iostream>
#include <boost/units/systems/si/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/base_units/metric/hour.hpp>
#include <boost/units/base_units/metric/minute.hpp>

using namespace std;
using namespace boost::units;
using namespace boost::units::si;
using namespace boost::units::metric;

int main()
{   
    cout <<
        static_cast<quantity<hour_base_unit::unit_type>>
        (quantity<time>{29.0 * minute_base_unit::unit_type()} + 60.0 * seconds)
        << endl;        

    return 0;
}

回答1:


You could just do

(minutes/60+seconds/3600)



来源:https://stackoverflow.com/questions/34385537/add-minutes-and-seconds-display-in-hours

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!