天计算在年和月? [英] Calculate days in years and months?
问题描述
如何计算天在年和月在c#?每例如
:
How to calculate days in years and months in c#? per example :
如果
1 天= 385
然后我需要显示年度= 1.1
(即1年2月)
2. 天= 234
然后我需要显示年= 0.7
(也就是0年7个月)
If
1. days = 385
then I need to display Year= 1.1
(i.e. 1 year 1 month)
2. days= 234
then I need to display year =0.7
(i.e 0 year 7 months)
我们如何在C#中计算?
How can we calculate in c#?
我为天=365分之234
键,结果来了0.64(即0年6个月)。但实际上这是7个月。
I did for days=234 /365
and result is coming 0.64 (i.e. 0 year 6 months). But actually it is 7 months.
如何获得准确的年份和月份。
How to get accurate year and months.
推荐答案
假设每月一年正好十二分之一,那你想忽略的部分个月(根据你说你期待7从你的榜样与7.688个月,则:
Assuming a month of exactly one-twelfth of a year, and that you want ignore partial months (based on your saying you expect 7 from your example with 7.688 months, then:
int days = 234;
double years = (double)days / 365.242199;
int wholeYears = (int)Math.Floor(years);
double partYears = years - wholeYears;
double approxMonths = partYears * 12;
string horribleFormat = string.Concat(wholeYears, ".", approxMonths);
这篇关于天计算在年和月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!