Suppose there is a variable containing the value in ms let ms = 300; , how can you convert milliseconds to floating point seconds and how to adjust the number of decimal places? For example, to be able to display 0.3 сек. or 0.300 сек. ?
- oneneed to be divided by 1000, the toFixed function allows you to set the number of decimal places - Grundy
- @Grundy thanks! - JamesJGoodwin
|
1 answer
An example of the millisecond conversion method to seconds:
function millisToSeconds(millis) { // toFixed(3) вернет 0.300; toFixed(1) вернет 0.3 var seconds = (millis / 1000).toFixed(3); return seconds + ' сек.'; } var seconds = millisToSeconds(300); alert(seconds); // результатом будет 0.300 сек. The
toFixed()method formats a number using a fixed-point entry.
By the way, by the way, here is a good example of converting milliseconds into minutes and seconds:
function millisToMinutesAndSeconds(millis) { var minutes = Math.floor(millis / 60000); var seconds = ((millis % 60000) / 1000).toFixed(0); return minutes + ":" + (seconds < 10 ? '0' : '') + seconds; } millisToMinutesAndSeconds(298999); // "4:59" millisToMinutesAndSeconds(60999); // "1:01" Reference to the source: Converting milliseconds to minutes and seconds with Javascript
|