有没有办法将 boost::date_duration 转换为双精度。我有以下代码:
date be;
date bd;
days t = (be - bd);
std::cout << "days are:" << t << std::endl;
这工作正常并返回否。天。但我想得到以年为单位的值,所以如果我将 t 除以 365,它只显示 0。setprecision()
也没有帮助。
有没有办法将 boost::date_duration 转换为双精度。我有以下代码:
date be;
date bd;
days t = (be - bd);
std::cout << "days are:" << t << std::endl;
这工作正常并返回否。天。但我想得到以年为单位的值,所以如果我将 t 除以 365,它只显示 0。setprecision()
也没有帮助。
您可能被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 天。