?
Solved

Live Money/Currency Counter (US Debt Clock)

Posted on 2011-10-30
16
Medium Priority
?
632 Views
Last Modified: 2012-06-27
Hi. I'm looking for a script to count money up including cents. Exactly like this website: http://costofwar.com/en/state/AL/congdistrict/06/ except I need it fancier with images as numbers and a background.

For example: I set it to start from $1,030,301.01 and it should count up every second by whatever amount. Also, it cannot reset on reload and should be live no matter who sees the page.

Thank you.
0
Comment
Question by:lean0nme
  • 8
  • 8
16 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37052621
So do you have something already and a server process that stores the current amount?
Or is this a Gimme-the-codez question?
0
 

Author Comment

by:lean0nme
ID: 37053186
I guess it's a gimme-the-codez question lol.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37053266
My email address is in my profile as well as a HIRE ME button
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:lean0nme
ID: 37053350
Ok, let's try it this way if you could help I'd appreciate it. I have this code to do what I want, but I need to modify 2 things.

1. It gets the value using date. I want to specificy the value.
2. I want the numbers to be fancier and use images instead.

Could you help with those please? :)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
	<title>Amount Incrementer</title>
	
	<script type="text/javascript">
		function calcAmount() {
		      var dateNow = new Date();
		      var dateStart = new Date();
			  var incAmount = .01; // change to the amount by which you wish to increment
		      dateStart.setDate(1);
		      dateStart.setMonth(0);
		      dateStart.setHours(0);
		      dateStart.setMinutes(0);
		      dateStart.setSeconds(0);
		      dateStart.setMilliseconds(0);
		      t = setTimeout(calcAmount, 1);
		      document.getElementById('amount').innerHTML = "$" + CurrencyFormatted(incAmount*parseInt((dateNow - dateStart)/(1000)));
		}
		
	// function for formatting numbers in currency format
	function CurrencyFormatted(amount)
	{
		var i = parseFloat(amount);
		if (isNaN(i)) {
			i = 0.00;
		}
		var minus = '';
		if (i < 0) { 
			minus = '-'; 
		}
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if (s.indexOf('.') < 0) { 
			s += '.00'; 
		}
		if (s.indexOf('.') == (s.length - 2)) { 
			s += '0'; 
		}
		s = minus + s;
		s = CommaFormatted(s)
		return s;
	}
	
	// function for formatting numbers with embedded commas
	function CommaFormatted(amount)
	{
		var delimiter = ","; // replace comma if desired
		var a = amount.split('.',2)
		var d = a[1];
		var i = parseInt(a[0]);
		if (isNaN(i)) { 
			return ''; 
		}
		var minus = '';
		if (i < 0) { 
			minus = '-'; 
		}
		i = Math.abs(i);
		var n = new String(i);
		var a = [];
		while(n.length > 3)
		{
			var nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		}
		if (n.length > 0) { 
			a.unshift(n); 
		}
		n = a.join(delimiter);
		if (d.length < 1) { 
			amount = n; 
		} else { 
			amount = n + '.' + d; 
		}
		amount = minus + amount;
		return amount;
	}
	</script>
	
</head>
 
<body onload="calcAmount();">
 
Current amount is: <span id="amount"></span>
 
</body>
</html>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37053407
Sure. I'll look tomorrow morning. NOW I'm off to celebrate my birthday.
0
 

Author Comment

by:lean0nme
ID: 37053422
Thanks and Happy birthday!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37055192
Thanks - I strongly advice against using images
Instead go wild with CSS.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
  <title>Amount Incrementer</title>
<style type="text/css">
#amount { font-face:courier; font-weight:bold; color:teal; }
</style>  
  <script type="text/javascript">
  
  // function for formatting numbers in currency format
  function CurrencyFormatted(amount)
  {
    var i = parseFloat(amount);
    if (isNaN(i)) {
      i = 0.00;
    }
    var minus = '';
    if (i < 0) { 
      minus = '-'; 
    }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { 
      s += '.00'; 
    }
    if (s.indexOf('.') == (s.length - 2)) { 
      s += '0'; 
    }
    s = minus + s;
    s = CommaFormatted(s)
    return s;
  }
  
  // function for formatting numbers with embedded commas
  function CommaFormatted(amount)
  {
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.',2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { 
      return ''; 
    }
    var minus = '';
    if (i < 0) { 
      minus = '-'; 
    }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while(n.length > 3)
    {
      var nn = n.substr(n.length-3);
      a.unshift(nn);
      n = n.substr(0,n.length-3);
    }
    if (n.length > 0) { 
      a.unshift(n); 
    }
    n = a.join(delimiter);
    if (d.length < 1) { 
      amount = n; 
    } else { 
      amount = n + '.' + d; 
    }
    amount = minus + amount;
    return amount;
  }
  var amount = 1030301.01; // load from server
  window.onload=function() {
    var delay = 100; // milliseconds    
    var incAmount = .01; // change to the amount by which you wish to increment
    var tId = setInterval(function() {
      document.getElementById('amount').innerHTML = "$" + CurrencyFormatted(amount+=incAmount);
    },delay)

  }
  </script>
  
</head>
 
<body>
 
Current amount is: <span id="amount"></span>
 
</body>
</html>

Open in new window

0
 

Author Comment

by:lean0nme
ID: 37060335
Any reason to use CSS instead of images? I want it to have like a "flip style" counter.

Also, the counter shouldn't reset after reload and should be "live" with everyone seeing the same number like on the http://costofwar.com/en/state/AL/congdistrict/06/ website.

Thanks for the help!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37060787
Yes, using images are VERY resource hungry when flipping - now we flip as ft as possible, with images we can perhaps flip once a second
It is up to you to have the page read the initial counter from a text file. It is not advisable to read the server counter more than once per page load.
You insert it where I suggested it
0
 

Author Comment

by:lean0nme
ID: 37060808
Can you please elaborate on that? In order for me to have a live counter, I would have to write to a txt file and read it constantly, no? How could I make it do it once refresh?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37060852
I do not know how precise this counter should be nor what the algorithm for the number. If it can be calculated, calculate it each time the page is loaded. If not have a script run on the server that updates a text file and read that file from the page that shows it to the user.

Otherwise WHO updates the counter? Each person that loads the page will call to the server every second or so? That could be very heavy.

Perhaps you tell me what exactly you are counting and how you get to that number and I can give better answers
0
 

Author Comment

by:lean0nme
ID: 37060864
It's not calculating anything. Just takes a number from a .txt file and displays it is good enough. The file needs to be updated every second to increment the number and I just need that number to be consistent with everyone who views the page.

So basically I could create a php script to constantly update a .txt file and read that number with the script you provided? Any ideas for doing that?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37060892
Sure
Not being a php guy I would change the bit where it says 1030301.01 to
<?php file_get("counter.txt"); ?>
0
 

Author Comment

by:lean0nme
ID: 37060913
Thanks. I can write a php script to constantly update counter.txt though a loop every second or so but wouldn't that load the server too much? Also, where would I put the script in order for it to run constantly?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 37061351
What can I say?

You want a value that all who load your page will see. If you cannot calculate it in the script, then of course you need a server file

If you have thousand of hits a minute, you will need to create the file and read it into memory when your server starts, keep it there and occasionally write a copy to file in case you need to restart the server.

It is very basic http. If you want a value to persist and update, you will need to store the value somewhere. If you need it to persist across reboots, you need a file.
0
 

Author Closing Comment

by:lean0nme
ID: 37067161
Thanks for all the help!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Before you approach an Ionic Mobile App development company for developing mobile apps using the ionic framework, you must know why you should choose the framework. Let's try to understand that through this article.
The viewer will learn how to count occurrences of each item in an array.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question