locked
Time conversion confusion RRS feed

  • Question

  • User-374939276 posted

    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.

    Monday, March 23, 2020 4:15 AM

Answers

  • User-719153870 posted

    Hi warrenkc2,

    warrenkc2

    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.

    To achieve this, you will need the toFixed() method.

    Update your code to:

    var milliseconds = (milliseconds % 1000).toFixed(1);

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 23, 2020 6:01 AM

All replies

  • User-719153870 posted

    Hi warrenkc2,

    warrenkc2

    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.

    To achieve this, you will need the toFixed() method.

    Update your code to:

    var milliseconds = (milliseconds % 1000).toFixed(1);

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 23, 2020 6:01 AM
  • User-374939276 posted

    I made a new function. 

    function formatSeconds(totalSeconds) {
      var hours = Math.floor(totalSeconds / 3600);
      totalSeconds %= 3600;
      var minutes = Math.floor(totalSeconds / 60);
      var seconds = Math.floor(totalSeconds) % 60;
      var milliseconds = Math.floor(totalSeconds * 1000) % 1000;
    
      return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds) + ',' + pad(milliseconds);
    }
    

    Monday, March 23, 2020 9:18 AM