# How to limit number display

• 02-28-2013, 04:59 PM
pastajon
How to limit number display
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;

var msInterval = INTERVAL * 1000;
var now = new Date();
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
setInterval(incrCount, msInterval);
}

function incrCount() {
count += INCREMENT;
}

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>
• 03-01-2013, 07:25 AM
007Julien
I understand ?
To add commas separator try this :
Code:

return (''+nStr).replace(/(\d{3})(?=\d)/g,'\$1,')
}

We replace all three digits followed by a non-captured digit (with a positive assertions ?=), by the three digits and a comma.
• 03-01-2013, 08:01 AM
mgrace
One way is to limit the possible options at server side.
• 03-01-2013, 10:37 AM
pastajon
Thanks, but this doesn't shorten my display number to three digits, e.g. 16.5
Quote:

Originally Posted by 007Julien
I understand ?
To add commas separator try this :
Code:

return (''+nStr).replace(/(\d{3})(?=\d)/g,'\$1,')
}

We replace all three digits followed by a non-captured digit (with a positive assertions ?=), by the three digits and a comma.

I want to shorten my display number to three digits, from 16,xxx,xxx,xxx,xxx to 16.x
• 03-01-2013, 10:42 AM
007Julien
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
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.
• 03-01-2013, 10:45 AM
JMRKER
Quote:

Originally Posted by pastajon
I want to shorten my display number to three digits, from 16,xxx,xxx,xxx,xxx to 16.x

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.
• 03-01-2013, 10:59 AM
pastajon
Where do I place number.toFixed(1) ?

Yes, I can be limited to 10.0 to 99.9.

Do I place it somewhere within this new string of code?

return (''+nStr).replace(/(\d{3})(?=\d)/g,'\$1,')
}
• 03-01-2013, 01:25 PM
JMRKER
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>
<title> Untitled </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<body>
<div id="counter"></div>

<script type="text/javascript">
// 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

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>

• 03-01-2013, 02:47 PM
pastajon
JMRKER,

Your solution accomplishes what I am wanting, a three digit display. Now I just need to work on the equation to reflect the rate I'm after:

The national debt grows at a rate of \$48,994 per second and I want the display to reflect the total in trillions, e.g. 16.6.

• 03-01-2013, 08:03 PM
JMRKER
You accuracy of trillion increments willl not change very much. :eek:
Here is an example of that and a millions display and actual (as much accuracy as computer will allow)

Code:

<!DOCTYPE HTML>
<html>
<title> Untitled </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<body>
<div id="counter"></div>
<div id="counterMillion"></div>
<div id="counterActual"></div>

<script type="text/javascript">

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

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>

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.10406 seconds
• Memory Usage 2,387KB
• Queries Executed 11 (?)
Template Usage (21):
• (5)bbcode_code_printable
• (2)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates