I'm a cut and paste script guy and play with variables, but write no code.
I'm using a count up script that I want to set how many digits / numbers are displayed. The variables use large numbers and for this particular display I want to use three digits and a decimal, e.g. 16.5 versus 16.00000000000000.
Here is my code:
var START_DATE = new Date("February 27, 2013 11:53:00"); // put in the starting date here
var INTERVAL = 1; // in seconds
var INCREMENT = .000000048994; // increase per tick
var START_VALUE = 16.6; // initial value when it's the start date
var count = 0;
window.onload = function() {
var msInterval = INTERVAL * 1000;
var now = new Date();
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
document.getElementById('counter').innerHTML = addCommas(count.toString());
setInterval(incrCount, msInterval);
}
function incrCount() {
count += INCREMENT;
document.getElementById('counter').innerHTML = addCommas(count.toString());
}
I want to shorten my display number to three digits, from 16,xxx,xxx,xxx,xxx to 16.x
How big are your numbers?
Do you want it to be limited to 10.0 to 99.9 only? If yes, use number.toFixed(1).
Above will limit decimal display to 1 digit and not limit the integer portion.
To display the number of seconds since a start date, this could be a answer:
Code:
var START_DATE = new Date("February 27, 2013 11:53:00"); // put in the starting date here
window.onload = function() {
incrCount();
setInterval(incrCount,1000);
}
function incrCount() {
count=Math.floor((new Date().valueOf()-START_DATE.valueOf())/1000)
document.getElementById('counter').innerHTML =(''+count).replace(/(\d)(?=(\d\d\d)+\b)/,'$1,');
}
Since the method count.toFixed(n) transform a number in a string with n digits after the point, while count=Math.floor(count*Math.pow(10,n))/Math.pow(10,n); preserve a number with at most n digits after the point.
Not sure you need addCommas() function if output is limited to xx.x display.
See commented line if this is your desire
Modified with recommendation from post #5 also.
Code:
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div id="counter"></div>
<script type="text/javascript">
// Modified for: http://www.webdeveloper.com/forum/showthread.php?273809-How-to-limit-number-display&p=1253049#post1253049
// with suggestions from post #5
var START_DATE = new Date("February 27, 2013 11:53:00"); // put in the starting date here
var START_VALUE = 16.6; // initial value when it's the start date
window.onload = function() {
incrCount();
setInterval(incrCount,1000);
}
function incrCount() {
count=Math.floor((new Date().valueOf()-START_DATE.valueOf())/1000) +START_VALUE;
count = count % 100; // use if display is to be limited to 0.0 to 99.9 only, otherwise remove line
document.getElementById('counter').innerHTML = count.toFixed(1);
}
</script>
</body>
</html>
You accuracy of trillion increments willl not change very much.
Here is an example of that and a millions display and actual (as much accuracy as computer will allow)
Code:
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div id="counter"></div>
<div id="counterMillion"></div>
<div id="counterActual"></div>
<script type="text/javascript">
// Modified for: http://www.webdeveloper.com/forum/showthread.php?273809-How-to-limit-number-display&p=1253049#post1253049
var START_DATE = new Date("February 27, 2013 11:53:00"); // put in the starting date here
var START_VALUE = 16600000000000; // initial value when it's the start date (16.6 trillion)
var Trillion = 1000000000000; // representation of 1 trillion
var incAmount = 48994; // debt increase per second
window.onload = function() {
incrCount();
setInterval(incrCount,1000);
}
function incrCount() {
var now = new Date();
count = (START_VALUE + ((now - START_DATE)/1000) * incAmount);
document.getElementById('counter').innerHTML = (count/Trillion).toFixed(1)+' trillion';
document.getElementById('counterMillion').innerHTML = (count/1000000).toFixed(1)+' million';
document.getElementById('counterActual').innerHTML = count.toFixed(1)+' dollars';
}
</script>
</body>
</html>
Bookmarks