开发者

converting boost::gregorian::date_duration to double

Is there a way to convert the boost::date_duration to a double. I have the following 开发者_如何学Pythoncode:

date be; 
date bd;
days t = (be - bd);
std::cout << "days are:" << t << std::endl;

This works fine and returns the no. of days. But I want to get the value in years, so if I divide t by 365, it only shows 0. setprecision() did not help either.


You're probably caught by the fact that boost::gregorian::days overloads operator/. Convert to an integer first using days() and then divide by a floating-point value to get floating-point division.

#include <iostream>
#include <boost/date_time.hpp>

int main()
{
    boost::gregorian::date be(2000, 1, 1), bd(1950, 1, 1);
    boost::gregorian::days t = be - bd;
    std::cout << "days are: " << t << '\n'
              << "days/365 = " << t.days()/365.0 << '\n';
}

Note the output: your result is not equal to the number of years because a year is not 365.00 days.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜