# JavaScript Decimal toFixed() Not Working

Page 1 of 1

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

### #1 Liam O'Byrne

Reputation: 0
• Posts: 41
• 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

Reputation: 134
• Posts: 868
• 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);
</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

### #3 andrewsw

• blow up my boots

Reputation: 6517
• Posts: 26,382
• 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>
<title>Page Title</title>
<style type="text/css">
/* */
</style>
<script type="text/javascript">
var start = new Date();
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>
<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.

### #4 andrewsw

• blow up my boots

Reputation: 6517
• Posts: 26,382
• 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

### #5 andrewsw

• blow up my boots

Reputation: 6517
• Posts: 26,382
• 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.

### #6 JMRKER

Reputation: 134
• Posts: 868
• Joined: 25-October 08

## Re: JavaScript Decimal toFixed() Not Working

Posted 10 April 2013 - 05:00 PM

andrewsw, 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.

### #7 Liam O'Byrne

Reputation: 0
• Posts: 41
• 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!

### #8 andrewsw

• blow up my boots

Reputation: 6517
• Posts: 26,382
• Joined: 12-December 12

## Re: JavaScript Decimal toFixed() Not Working

Posted 11 April 2013 - 09:13 AM

Liam 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.

### #9 Liam O'Byrne

Reputation: 0
• Posts: 41
• 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);

```

### #10 andrewsw

• blow up my boots

Reputation: 6517
• Posts: 26,382
• Joined: 12-December 12

## Re: JavaScript Decimal toFixed() Not Working

Posted 11 April 2013 - 10:36 AM

Liam 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);
```

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.

### #11 JMRKER

Reputation: 134
• Posts: 868
• 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>
<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
// eliminate t.padDigit() above if days display not desired

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

<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

### #12 Liam O'Byrne

Reputation: 0
• Posts: 41
• 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.

### #13 JMRKER

Reputation: 134
• Posts: 868
• 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!