2014-06-05 42 views

回答

2

使用整數除以60來將秒數轉換爲整個分鐘。秒數的模數(%)爲60會給出「剩餘」秒數。然後使用字符串轉換來檢查0填充的必要性。

int minutes = (total/60); 
int seconds = (total % 60); 
String secs = Integer.toString(seconds); 
if (seconds < 10) { 
    secs = "0" + seconds; 
} 
String time = minutes + ":" + secs; 
+1

+1雖然你有不必要的投射到'int' – alfasin

+0

沒有任何意義浪費空間來存放長時間。真相被告知,我把原文翻譯爲雙重,因此是演員。現在修好。 –

0

一些算術:

long min = value/60; 
long second = value % 60 ; 
String value = min + ":" + (second > 10 ? "" : "0") + second; 
6
String readable = String.format("%d:%02d", s/60, s%60); 
+2

+1這更優雅! – alfasin

+1

+1內置庫和更簡單的代碼。 –

0
int duration = 90; 
int min = duration/60; 
int sec = duration%60; 
String formattedTime = String.format("%d:%02d",min,sec);