如何获得基于总数的年数和月数?

时间:2019-11-13 01:28:51

标签: c#

我想根据总数来计算多少年和几个月。这是我到目前为止所做的。

int total = 23;
float test = total/12;

entYears.Text = //number of years here;
entMonths.Text = //number of months here;

2 个答案:

答案 0 :(得分:3)

年数将为total/12。另一方面,月份将为total%12

一年是12个月,因此要使用年份除法,要获得年数。计算年份后,月份将是“剩余”,这是模数12起作用的地方。

23/12的计算结果为1,因此为1年。 23%12的评估值为11。这意味着23是1年11个月。

答案 1 :(得分:0)

您需要使用remainder operatorobject):

%

Try it online