transform float number to seconds js

Solutions on MaxInterview for transform float number to seconds js by the best coders in the world

showing results for - "transform float number to seconds js"
Amy
03 Apr 2016
1function convertNumToTime(number) {
2    // Check sign of given number
3    var sign = (number >= 0) ? 1 : -1;
4
5    // Set positive value of number of sign negative
6    number = number * sign;
7
8    // Separate the int from the decimal part
9    var hour = Math.floor(number);
10    var decpart = number - hour;
11
12    var min = 1 / 60;
13    // Round to nearest minute
14    decpart = min * Math.round(decpart / min);
15
16    var minute = Math.floor(decpart * 60) + '';
17
18    // Add padding if need
19    if (minute.length < 2) {
20    minute = '0' + minute; 
21    }
22
23    // Add Sign in final result
24    sign = sign == 1 ? '' : '-';
25
26    // Concate hours and minutes
27    time = sign + hour + ':' + minute;
28
29    return time;
30}
31
32console.log(convertNumToTime(11.15));
33console.log(convertNumToTime(1.08));
34console.log(convertNumToTime(-2.50));
35console.log(convertNumToTime(2));
36console.log(convertNumToTime(0.40));