12 Replies - 1094 Views - Last Post: 11 April 2013 - 12:52 PM

#1 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 02-April 13

JavaScript Decimal toFixed() Not Working

Posted 10 April 2013 - 08:28 AM

I have provided a link jsFiddle so you can see my problem (http://jsfiddle.net/QVBep/1/).

I have a simple calculator which calculates the duration from start to stop.

This works perfectly well, however, at present, it returns a value like so (12:05.4444444444444444444).

My aim is to chop off those irrelitive decimal places to return value like so (12:05.04).

I have tried using
.toFixed()
and
Math.round()
, however this made no change.

Any help would be greatly appreciated! Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript Decimal toFixed() Not Working

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: JavaScript Decimal toFixed() Not Working

Posted 10 April 2013 - 01:05 PM

Your 'jsFiddle' code does not work.

Corrected syntax is:
<script type="text/javascript">
function collectTime() { var sTime = new Date(); return sTime; }

// Duration Calculator
var s = collectTime();
// following is artificial delay to put some time between start() and stop()
var j=0; for (var i=1000; i<Math.floor(Math.random()*1000); i++) { j++; }
var e = collectTime();
var difference = e-s;

var diffSeconds = difference / 1000;

var HH = Math.floor(diffSeconds / 3600);
var MM = Math.floor(diffSeconds % 3600) / 60;

var formatted = ((HH < 10) ? ("0" + HH) : HH) 
              + ":" + ((MM < 10) ? ("0" + MM) : MM);
alert(formatted);
</script>


But it does not demonstrate your problem because the actions between start to stop are faster than 1 second which is the resolution to which you display.

Therefore, the only display you get from above is '00:00' and does not demonstrate your problem.

This post has been edited by JMRKER: 10 April 2013 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: JavaScript Decimal toFixed() Not Working

Posted 10 April 2013 - 02:21 PM

I don't see the need for functions like start() and stop() as they do so little. You might find the following a little easier to test although TBH I haven't tested it thoroughly myself.

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
    <style type="text/css">
        /* */
    </style>
    <script type="text/javascript">
        var start = new Date();
        //alert('pause here');
        var stop = new Date();
        var stop = stop.setHours(23, 38, 22); // artificial delay
        var diffSecs = (stop - start) / 1000;   // in seconds
        var hrs = Math.floor(diffSecs / 3600);
        var removedHrs = diffSecs % 3600;
        var mins = Math.floor(removedHrs / 60);
        var removedMins = removedHrs % 60;
        var secs = Math.floor(removedMins);
        hrs = ((hrs < 10) ? "0" : "") + hrs;
        mins = ((mins < 10) ? "0" : "") + mins;
        secs = ((secs < 10) ? "0" : "") + secs;
        alert(hrs + ":" + mins + ":" + secs);
    </script>
</head>
<body>
</body>
</html>

The idea is that you can use the setHours(hh, mm, ss) call to create the delay or (if you have the patience :) ) comment-out this line and uncomment the alert('pause here') line.

With apologies to JMRKER; just offering an alternative for the OP.
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: JavaScript Decimal toFixed() Not Working

Posted 10 April 2013 - 02:28 PM

Although, @JMRKER, your delay will not be long enough.

Actually, @JMRKER, your version has the same issue as the OP-code, in that the minutes will still display a fractional part.

This post has been edited by andrewsw: 10 April 2013 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: JavaScript Decimal toFixed() Not Working

Posted 10 April 2013 - 02:58 PM

BTW @OP The reason toFixed() and Math.round() do not work is that the value 12:05.4444444444444444444 is already a string (as it contains the colon) and not a number, which these functions expect.
Was This Post Helpful? 0
  • +
  • -

#6 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: JavaScript Decimal toFixed() Not Working

Posted 10 April 2013 - 05:00 PM

View Postandrewsw, on 10 April 2013 - 03:21 PM, said:

...
With apologies to JMRKER; just offering an alternative for the OP.


No problem for me ... whatever helps the OP, I assume helps all. :)

My suggestion did not solve the OP's problem.
I was just pointing out the syntax errors that needed to be fixed before further testing could progress.
Was This Post Helpful? 0
  • +
  • -

#7 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 02-April 13

Re: JavaScript Decimal toFixed() Not Working

Posted 11 April 2013 - 09:03 AM

Thanks for your time and effort JMRKER andrewsw.

However, sometimes (not all the time) when I check the results of the time it returns (for example: 00:01:97).

Is the 97 seconds milliseconds or seconds? And is it possible to remove the milliseconds/seconds and leave just HH:MM?

Thanks once again!
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: JavaScript Decimal toFixed() Not Working

Posted 11 April 2013 - 09:13 AM

View PostLiam O, on 11 April 2013 - 04:03 PM, said:

Thanks for your time and effort JMRKER andrewsw.

However, sometimes (not all the time) when I check the results of the time it returns (for example: 00:01:97).

Is the 97 seconds milliseconds or seconds? And is it possible to remove the milliseconds/seconds and leave just HH:MM?

Thanks once again!

I don't know what the 97 represents without seeing the code you are using. From my example, I would just not display the secs value, just hrs + ":" + mins.
Was This Post Helpful? 0
  • +
  • -

#9 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 02-April 13

Re: JavaScript Decimal toFixed() Not Working

Posted 11 April 2013 - 10:14 AM

Here is my code:

    // Duration Calculator
    var difference = endTime - startTime;
    var diffSeconds = difference / 1000;
    var HH = Math.floor(diffSeconds / 3600);
    var MM = (Math.floor(diffSeconds % 3600) / 60).toFixed(2);
    var duration = ((HH < 10) ? ("0" + HH) : HH) + ":" + ((MM < 10) ? ("0" + MM) : MM);


Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

Re: JavaScript Decimal toFixed() Not Working

Posted 11 April 2013 - 10:36 AM

View PostLiam O, on 11 April 2013 - 05:14 PM, said:

Here is my code:

    // Duration Calculator
    var difference = endTime - startTime;
    var diffSeconds = difference / 1000;
    var HH = Math.floor(diffSeconds / 3600);
    var MM = (Math.floor(diffSeconds % 3600) / 60).toFixed(2);
    var duration = ((HH < 10) ? ("0" + HH) : HH) + ":" + ((MM < 10) ? ("0" + MM) : MM);


Well, you are using toFixed(2) which converts a number to a string with two decimal places. So you could use .toFixed() which defaults to 0 decimal places. Or,

    var MM = Math.floor((diffSeconds % 3600) / 60);

or re-read my previous example-code.

You should take the time to study whichever code you decide to use, so that you understand it and are able to change it if necessary.
Was This Post Helpful? 1
  • +
  • -

#11 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: JavaScript Decimal toFixed() Not Working

Posted 11 April 2013 - 11:10 AM

See if this makes any sense to you...
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
Number.prototype.padDigit = function() { return (this < 10) ? '0'+this : this; }

function TimePassed(t,bflag) {                    // t is integer value of milliseconds of time
  var ms = t % 1000;                              // form milliseconds
  t = parseInt(t / 1000);                         // convert to integer value of seconds of time
  var ss = t % 60;  t = parseInt(t / 60);         // get seconds
  var mm = t % 60;  t = parseInt(t / 60);         // get minutes
  var hh = t % 24;  t = parseInt(t / 24);         // get hours

// form value of days:hh:mm:ss
  var str = t.padDigit()+':'+hh.padDigit()+':'+mm.padDigit()+':'+ss.padDigit();
// eliminate t.padDigit() above if days display not desired

  if (bflag != null) { str += ':'+ms.padDigit(); } // add in :ms, if desired
  return str;
}
</script>

</head>
<body>
Time difference in integer value of milliseconds<p><br>
<input type="text" id="Difference" value="12345678">
<button onclick="showTimeDifference()">Show time difference</button><p>

<table border="0">
<tr><td>Days:HH:MM:SS</td>    <td><input type="text" id="timeInfoHHMMSS" value=""></td></tr>
<tr><td>Days:HH:MM:SS:MS</td> <td><input type="text" id="timeInfoHHMMSSMS" value=""></td></tr>
</table>

<script type="text/javascript">
function showTimeDifference() {
  var diff = document.getElementById('Difference').value;
  document.getElementById('timeInfoHHMMSS').value = TimePassed(diff);
  document.getElementById('timeInfoHHMMSSMS').value = TimePassed(diff,true);
}
</script>

</body>
</html>


It is intended as a proof of concept script to show how the function would work
and show the returned (formatted) information about the time difference in milliseconds
:bananaman:
Was This Post Helpful? 1
  • +
  • -

#12 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 02-April 13

Re: JavaScript Decimal toFixed() Not Working

Posted 11 April 2013 - 11:38 AM

Thank you for your time and effort. I do understand it a bit better now thanks to your explanation above.
Was This Post Helpful? 0
  • +
  • -

#13 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: JavaScript Decimal toFixed() Not Working

Posted 11 April 2013 - 12:52 PM

You're most welcome.
Happy to help, once I understand the question.
Good Luck!
:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1