2017-01-03 150 views
0

我無法弄清楚如何使這個函數不會產生編譯器錯誤。typescript - 類型'數字'的參數不能分配給'string'類型的參數

該函數將在毫秒的持續時間和前置小時/分/秒與0,如果數量少於10

private msToTime(duration:any):string { 
    var milliseconds:string = String(parseInt((duration%1000)/100)) 
    var seconds:string = String(parseInt((duration/1000)%60)) 
    var minutes:string = String(parseInt((duration/(1000*60))%60)) 
    var hours:string = String(parseInt((duration/(1000*60*60))%24)) 

    hours = (parseInt(hours) < 10) ? "0" + hours : hours; 
    minutes = (parseInt(minutes) < 10) ? "0" + minutes : minutes; 
    seconds = (parseInt(seconds) < 10) ? "0" + seconds : seconds; 

    return hours + ":" + minutes + ":" + seconds; 
} 

回答

2

試過下面的代碼和編譯器不會返回任何錯誤。

private msToTime(duration:any):string { 
    var milliseconds:string = String((duration%1000)/100) 
    var seconds:string = String((duration/1000)%60) 
    var minutes:string = String((duration/(1000*60))%60) 
    var hours:string = String((duration/(1000*60*60)%24) 

    hours = (parseInt(hours) < 10) ? "0" + hours : hours; 
    minutes = (parseInt(minutes) < 10) ? "0" + minutes : minutes; 
    seconds = (parseInt(seconds) < 10) ? "0" + seconds : seconds; 

    return hours + ":" + minutes + ":" + seconds; 
} 
0

你嘗試調用parseInt((duration%1000)/100),其中(duration%1000)/100爲int了,所以你可以放棄這個方法,並使用

var milliseconds:string = String((duration%1000)/100) 
var seconds:string = String((duration/1000)%60) 
var minutes:string = String((duration/(1000*60))%60) 
var hours:string = String((duration/(1000*60*60))%24) 
0

parseInt()需要一個字符串輸入。因爲在這裏你的目的,使用Math.floor()

-1

類型的parseInt函數函數參數字符串 這樣,你可以這樣做: VAR毫秒:字符串=字符串(parseInt函數((持續時間1000%)/ 100)的ToString() );

相關問題