dharmatech
dharmatech

Reputation: 9527

Add minutes and seconds. Display in hours

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;
}

Upvotes: 2

Views: 683

Answers (1)

Matt Prodani
Matt Prodani

Reputation: 109

You could just do

(minutes/60+seconds/3600)

Upvotes: 0

Related Questions