2012-09-03 76 views
0

正如上面所說... 我不需要秒,因爲它不會不斷更新。 我已經搜索了這個網站和谷歌幾個小時,並找不到任何完全符合我需要的東西。 這裏是我的代碼:如何將double值轉換爲hh:mm?

//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)都是以小時爲單位。

+1

[模量算術](http://en.wikipedia.org/wiki/Modulo_operation) - 在C的語言''% 。 –

回答

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 
+0

Thnx,完美的作品! –