The output should be: something like: 00:00:01,25.099
I don't think it shall output 00:00:01,25.099, according to your code:
function formatMilliseconds(milliseconds) {
var seconds = Math.floor(milliseconds / 1000) % 60;
var minutes = Math.floor(seconds / 60) % 60;
var hours = Math.floor(minutes / 60);
var milliseconds = milliseconds % 1000;
return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds) + ',' + pad(milliseconds);
}
In general math, the milliseconds should be 25.1 after milliseconds % 1000, so the final output should be 00:00:01,25.1 or if you prefer 00:00:01,25.100.
Member
27 Points
187 Posts
Time conversion confusion
Mar 23, 2020 04:15 AM|warrenkc2|LINK
If I convert 1025.1 to the format hh:mm:ss,ms there seems to be a weird issue. The output is: 00:00:01,25.09999999999991
https://jsfiddle.net/warrenkc/wujLk5ap/1/
The output should be: something like: 00:00:01,25.099
Thank you for your help.
Contributor
3140 Points
983 Posts
Re: Time conversion confusion
Mar 23, 2020 06:01 AM|Yang Shen|LINK
Hi warrenkc2,
I don't think it shall output 00:00:01,25.099, according to your code:
In general math, the milliseconds should be 25.1 after milliseconds % 1000, so the final output should be 00:00:01,25.1 or if you prefer 00:00:01,25.100.
To achieve this, you will need the toFixed() method.
Update your code to:
or toFixed(3), choose whichever you like.
You can find more information about why it returns the wrong value at Is floating point math broken?
Best Regard,
Yang Shen
Member
27 Points
187 Posts
Re: Time conversion confusion
Mar 23, 2020 09:18 AM|warrenkc2|LINK
I made a new function.