【问题标题】:converting boost::gregorian::date_duration to double将 boost::gregorian::date_duration 转换为 double
【发布时间】:2011-08-29 20:15:11
【问题描述】:

有没有办法将 boost::date_duration 转换为双精度值。 我有以下代码:

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

这工作正常并返回否。天。但我想得到以年为单位的值,所以如果我将 t 除以 365,它只显示 0。setprecision() 也没有帮助。

【问题讨论】:

    标签: c++ boost boost-date-time


    【解决方案1】:

    您可能被boost::gregorian::days 重载operator/ 的事实所吸引。先用days()转成整数,再除以一个浮点值,得到浮点除法。

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

    注意输出:您的结果不等于年数,因为一年不是 365.00 天。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多