2 Replies - 392 Views - Last Post: 19 January 2012 - 06:36 PM

Topic Sponsor:

#1 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 175
  • View blog
  • Posts: 1,505
  • Joined: 20-January 09

Math.round() adding leading zeros

Posted 17 January 2012 - 03:40 PM

So here's my JS code:

<script type="text/javascript">
var m$ = jQuery.noConflict();
m$(document).ready(function(){
	num =  623000;
	prev = 623556;	
	subtract = num - prev;
	subtract /= 24;
	subtract /= 60;
	subtract /= 60;	
	
	var timerID = setInterval(function() {
		 if(num > 0){
			subtract *= 1000;
			subtract = Math.round(subtract);
			subtract /= 1000;
			num -= subtract;
			num *= 10000;
			num /= 10000;
			num = Math.round(num).toFixed(3);
			m$('.dynamic').html(addCommas(num));
		 }
		 else clearInterval(timerID);
    }, 1000 );

});

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


</script>


It a ticker that updates every second by subtracting the variable subtract from num. Unfortunately it no longer ticks down. I had it ticking just fine when I wasn't trying to do toFixed() to keep the zeros in the decimal place.

I Googled this and it said that I should use a string by doing the following: numstr = Math.round(num + "").toFixed(3);

And that didn't work either, at one point I was getting NaN.

Anyways, any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Math.round() adding leading zeros

#2 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 124
  • View blog
  • Posts: 552
  • Joined: 30-October 10

Re: Math.round() adding leading zeros

Posted 19 January 2012 - 04:14 PM

I would recommend reading about Number.toFixed() and Math.round().

Your explanation and code sample alone are not clear enough for me to understand what you're actually trying to do, but I am sure that you're making it more complicated that it needs to be! :)
Was This Post Helpful? 0
  • +
  • -

#3 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 99
  • View blog
  • Posts: 653
  • Joined: 25-October 08

Re: Math.round() adding leading zeros

Posted 19 January 2012 - 06:36 PM

This is my guess as to what you are trying to accomplish.
<html>
<head>
<title> Undefined </title>
<script type="text/javascript">
// From: 

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); }
	return x1 + x2;
}
var n = 100000000/3;	// numeric
var ns = n.toFixed(2);	// string
alert(addCommas(n)+'\n'+addCommas(ns));

    n = 200000000/3;	// numeric
    ns = n.toFixed(2);	// string
alert(addCommas(n)+'\n'+addCommas(ns));

</script>
</head>
<body>

</body>
</html>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1