【MT5】格式化显示时间函数 *天 *时 *分 *秒
具体步骤如下:
1.首先检查秒数是否小于等于0,如果是,则直接返回字符串"0"。
2.计算天数:将总秒数除以一天的秒数(24 * 3600)并取整。
3.计算剩余的小时数:取总秒数对一天的秒数取模,然后除以3600(一小时的秒数)并取整。
4.计算剩余的分钟数:取总秒数对一小时的秒数(3600)取模,然后除以60(一分钟的秒数)并取整。
5.计算剩余的秒数:取总秒数对60取模。
6.然后根据计算出的天数、小时、分钟和秒数构建一个字符串。如果某个单位的值大于0,则将其添加到结果字符串中,并且加上相应的单位(天、小时、分、秒)。
7.如果所有单位都是0(即总秒数为0),则返回"0秒"(但实际上前面已经判断了seconds<=0时返回"0",所以这里不会出现0秒的情况,除非seconds为0,但已经被返回了"0")。
函数一:
// 格式化时间显示
string FormatTime(double seconds)
{
if(seconds <= 0) return "0";
int days = (int)seconds / (24 * 3600);
int hours = ((int)seconds % (24 * 3600)) / 3600;
int mins = ((int)seconds % 3600) / 60;
int secs = (int)seconds % 60;
string result = "";
if(days > 0) result += IntegerToString(days) + "天 ";
if(hours > 0) result += IntegerToString(hours) + "小时 ";
if(mins > 0) result += IntegerToString(mins) + "分 ";
if(secs > 0 || result == "") result += IntegerToString(secs) + "秒";
return result;
}函数二:
// 格式化持仓时间显示
string FormatHoldingTime(double seconds)
{
int days = (int)seconds / (24 * 3600);
int hours = ((int)seconds % (24 * 3600)) / 3600;
int mins = ((int)seconds % 3600) / 60;
return IntegerToString(days) + "天 " + IntegerToString(hours) + "小时 " + IntegerToString(mins) + "分钟";
}


