# 将几分钟转变为小时[英] Transform minutes into hours

### 问题描述

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YourGold
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to YourGold App! \n------------------------");
Console.WriteLine("Your gold is : " + gold);
Console.WriteLine("Inesrt your time(In Hours) played: ");
int minutes = 60;
float time = (float)hours * minutes; // Here the calculation are wrong...
Console.WriteLine("Your total time playd is : " + time + " minutes");
float goldMin = gold / time;
Console.WriteLine("Your gold per minute is : " + goldMin);
Console.WriteLine("The application has ended, press any key to end this app. \nThank you for using it.");

}
}
}
```

p.s它与此问题有关:仅允许插入数字并将小时转换为几分钟来计算金/最小值 - 更新，我将其更新与此相同，但我认为我应该像现在这样做一个新问题(我' m仍在学习如何继续使用此平台:))

## 推荐答案

```var hours = 1.2;
var minutes = ((int)hours) * 60 + (hours%1)*100;
```

## 其他推荐答案

```TimeSpan time = TimeSpan.FromHours(1.2);
double minutes = time.TotalMinutes;
```

TimeSpan.FromHours 方法方法返回一个时间pan代表指定数量的小时数，其中规范准确至最近的毫秒.

```// string timeAsString = "1:20";
TimeSpan time;
if (TimeSpan.TryParse(timeAsString, CultureInfo.InvariantCulture, out time))
{
double minutes = time.TotalMinutes;
//... continue
}
else
{
// Ask user to input time in correct format
}
```

```var time = new TimeSpan(0, 1, 20, 0);
double minutes = time.TotalMinutes;
```

## 其他推荐答案

```time = (int)hours * 60 + (hours%1)*100
```

### 问题描述

I've got stuck in my program, i need to calculate the gold/minute but my math formula won't do the desire thing. As I input the hours into a float(something like 1.2 hours) the transformation will be 72 min instead of 80 as I need. Can you please help me ? I marked in comment below where the problem is. And here is my code :

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YourGold
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to YourGold App! \n------------------------");
Console.WriteLine("Your gold is : " + gold);
Console.WriteLine("Inesrt your time(In Hours) played: ");
int minutes = 60;
float time = (float)hours * minutes; // Here the calculation are wrong...
Console.WriteLine("Your total time playd is : " + time + " minutes");
float goldMin = gold / time;
Console.WriteLine("Your gold per minute is : " + goldMin);
Console.WriteLine("The application has ended, press any key to end this app. \nThank you for using it.");

}
}
}
```

Thanks a lot.

P.S it's related to this question:Allow only numbers to be inserted & transform hours to minutes to calculate gold/min - UPDATED , I update it same as this but i think i should have done a new question as i did now(I'm still learning how to go on with this platform:) )

## 推荐答案

```var hours = 1.2;
var minutes = ((int)hours) * 60 + (hours%1)*100;
```

And a side note: such way of inputting time is IMO not a good one. It'll be confusing and I guess that more often than not people will be actually entering 1:20 instead of 1.2, which'll break your application. And if not, they might be entering 1.5 thinking of 90 minutes. I know I would have done it like that.

## 其他推荐答案

Use the built-in TimeSpan:

```TimeSpan time = TimeSpan.FromHours(1.2);
double minutes = time.TotalMinutes;
```

TimeSpan.FromHours Method Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.

You can also do:

```// string timeAsString = "1:20";
TimeSpan time;
if (TimeSpan.TryParse(timeAsString, CultureInfo.InvariantCulture, out time))
{
double minutes = time.TotalMinutes;
//... continue
}
else
{
// Ask user to input time in correct format
}
```

Or:

```var time = new TimeSpan(0, 1, 20, 0);
double minutes = time.TotalMinutes;
```

## 其他推荐答案

If you really want your program to behave as you want do this.

```time = (int)hours * 60 + (hours%1)*100
```