0

正如上面所说......我不需要秒,因为它不会不断更新。我已经在这个网站和谷歌上搜索了几个小时,但找不到与我需要的完全一样的东西。这是我的代码:

//Sets the percentage calculation
double percent = level / 100;
//Calculates the time left while using the following
double a = 40 * percent;
double b = 400 * percent;
double c = 7 * percent;
double d = 6 * percent;

aTime.text = [NSString stringWithFormat:@"%f", a];
bTime.text = [NSString stringWithFormat:@"%f", b];
cTime.text = [NSString stringWithFormat:@"%f", c];
dTime.text = [NSString stringWithFormat:@"%f", d];

经过计算,比如说,7 小时的 50%,它目前给出了 3.5 小时。我想让它说 3:30。

如您所见,计算是静态的减去水平计算,我只需要知道如何将小数转换为 hh:mm(对于 400 小时来说,甚至可能是几天)。

注意:以上所有数字(40、400、7、6)均以小时为单位。

4

1 回答 1

3

例子:

double hours = 7; 
double percent = 0.5; // 50 percent
int value = hours * percent * 60; // value in minutes
NSString *formatted = [NSString stringWithFormat:@"%02d:%02d",value/60,value%60];
NSLog(@"%@",formatted);

输出:

03:30
于 2012-09-03T21:31:05.167 回答