将TimeSpan转换为分钟和秒 [英] Convert TimeSpan to Minutes and seconds

查看:2272
本文介绍了将TimeSpan转换为分钟和秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要解决两个问题.

首先,我想将TimeSpan转换为以下格式的分钟和秒:
如果是54分32秒,则它应该是54.32(两倍).

之后,我需要从totalDuration中减去值8.0.

我的代码如下所示:

public double TotalDuration结果,{结果,获取搜索结果{INT分钟;结果,INT秒;结果,时间跨度持续时间=结束时间 - 开始时间;结果,totalDuration = Convert.ToDouble(duration.TotalMinutes(分,秒))-8;
返回totalDuration;
}
设置{totalDuration = value; }
}


其次,我想对此值进行四舍五入.如果秒值大于30,则将其四舍五入.如果秒值小于30,则将其四舍五入.

例如:
如果值为54.32,则将其四舍五入为55.
如果值为54.20,则将其四舍五入为54.

Hi,

I need help with two issues.

Firstly, I want to convert a TimeSpan to minutes and seconds in this format:
If it is 54min 32sec, then it should be 54.32 (as double).

After which I need to subtract the value 8.0 from the totalDuration

This is how my code looks like:

public double TotalDuration
        {
            get
            {
                int minutes;
                int seconds;
                TimeSpan duration = EndTime - StartTime;
                totalDuration = Convert.ToDouble(duration.TotalMinutes(minutes,seconds)) - 8;
                return totalDuration;
            }
            set { totalDuration = value; }
        }


Secondly, I want to do a rounding to this value. If the seconds value is more than 30, round it up. If a seconds value is less than 30, round it down.

For example:
If a value is 54.32, round it up to 55.
If a value is 54.20, round it down to 54.

请指导我.我对此很陌生.谢谢.

Please guide me through this. I'm very new to this. Thanks.

推荐答案

您确实意识到54.32的两倍与54分钟的32秒不一样吗? 54分32秒更像是52.51的两倍?如果您想减去8分钟,为什么不又返回另一个时间跨度呢?您可以执行以下操作:

返回totalDuration-新的TimeSpan(0,8,0);
对于四舍五入,您可以执行以下操作:

int seconds = totalDuration.Seconds;
if(seconds> == 30){ int数量= 60-totalDuration.Seconds;
totalDuration = totalDuration.Add(新的TimeSpan(0,0,amount)) ;
}

else {
int量= totalDuration.Seconds;
totalDuration = totalDuration.Add(new TimeSpan(0,0,-amount));
}

You do realize that 54.32 as a double is NOT the same as 54 minutes and 32 second right? 54 minutes and 32 seconds is more like 52.51 as a double? If you want to subtract 8 minutes, why don't you just return another timespan? You can just do this:

return totalDuration - new TimeSpan(0,8,0);

As for the rounding, you can just do something like this:

int seconds = totalDuration.Seconds;
if(seconds >= 30) {
   int amount = 60 - totalDuration.Seconds;
   totalDuration = totalDuration.Add(new TimeSpan(0,0,amount));
}

else {
   int amount = totalDuration.Seconds;
   totalDuration = totalDuration.Add(new TimeSpan(0,0,-amount));
}


这篇关于将TimeSpan转换为分钟和秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆