如何根据给定费率(付款)生成时间(以小时和分钟为单位)?用户需要输入付款和费率才能获得总持续时间。如何完成这项任务?
示例:
rate = $2.00 per hours
payment = $5.00
结果:
duration = 02 h 30 minutes
谢谢。
答案 0 :(得分:2)
使用TimeSpan
简化您的代码......
Private Function GetDuration(hourlyRate as decimal, payment as decimal) As TimeSpan
return TimeSpan.FromHours(payment / hourlyRate)
End Function
' ...
Dim duration = GetDuration(2, 5)
Console.WriteLine("{0} hours {1} minutes", duration.Hours, duration.Minutes)
答案 1 :(得分:1)
hours = payment \ rate;
minutes = (payment/rate - hours) * 60;
答案 2 :(得分:1)
一旦你有小时数的小数点表示(即2.5),你所要做的就是:
这些方面应该有效:
Dim hours as Integer
Dim minutes as Integer
Dim time as Double
time = payment / rate
hours = CInt(time)
minutes = CInt((time - hours) * 60)
Console.WriteLine("duration = {0} h {1} minutes", hours, minutes)
答案 3 :(得分:1)
使用TimeSpan
结构。它将为您计算小时和分钟。
Dim rate As Double = 2.0
Dim payment As Double = 5.0
Dim time = TimeSpan.FromHours(payment / rate)
Dim duration = [String].Format("{0:00} h {1:00} minutes", time.Hours, time.Minutes)
' ==> "02 h 30 minutes"
您在使用资金时也可以使用Decimal
类型。它具有在从二进制格式转换时不引入额外舍入误差的优点。但是,TimeSpan.FromHours
预计小时数为Double
。因此Double
似乎适合这个小例子。