本文介绍了在C#中获取最后的'N'个季度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设当前季度为3,年份为2011.如何获得最近5个季度
Suppose the current quater is 3 and the year is 2011. How can I get the last 5 quarters
所需的输出:
Q3-2011
Q2-2011
Q1-2011
Q4-2010
Q3-2010
Q和'-'被附加.
我正在尝试下
int generateQuater = 5;
int currentQuater = 3;//GetQuarter(DateTime.Now.Month);
int currentYear = DateTime.Now.Year;
List<string> lstQuaterYear = new List<string>();
lstQuaterYear.Add(string.Concat('Q',currentQuater, '-', currentYear));
for (int i = generateQuater; i > 0; i++)
{
//code to be placed
}
谢谢
推荐答案
您必须减少您的循环变量.其余的数学并不是太困难.也不必以任何特殊方式处理第一次迭代:
You have to decrease your loop variable. The rest is not too difficult math.Its also not necessary to handle the first iteration in any special way:
for (int i = generateQuater; i > 0; i--)
{
lstQuaterYear.Add(string.Format("Q{0}-{1}", currentQuater, currentYear));
if (--currentQuater == 0)
{
currentQuater = 4;
currentYear--;
}
}
这篇关于在C#中获取最后的'N'个季度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!