Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-26-2012, 02:54 AM   PM User | #1
computerguy
New to the CF scene

 
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
computerguy is an unknown quantity at this point
Question How do I fix this?

Can some one please help me with my java script?
My java script consistently counts up, the problem is the cents character length is way too long (need 2 characters max). Another problem is, I don't know what code additionally I need to put commas in the correct position for determining the proper amount. Example: 12345.67 vs 12,345.67. If some one can just take a look at the code, modify it and re-post the full code since I have no idea what to do, I would deeply appreciate it.

http://jsfiddle.net/pqsH6/
computerguy is offline   Reply With Quote
Old 06-26-2012, 03:56 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,578
Thanks: 62
Thanked 4,062 Times in 4,031 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Next time, just post the code here instead of in some other place.

Your code is both overkill and underkill.

Code:
<html>
<head>
<style type="text/css">
#counter {
    margin:0px 0px 0px 0px;
    float: left;
}
#ds {
    margin:0px 0px 0px 7px;
    float: left;
    font-weight: bold;
}
</style>
</head>
<body>
<p style="float:left;">Money Saved: </p>
<p id="ds">$</p><div id="counter"></div>
<script type="text/javascript">
var START_DATE = new Date(2012,0,1,12,0,0); // january 1, 2012 12:00:00
    
var INTERVAL = 1000; // savings per second
var INCREMENT = 0.005; // money saved per second
var START_VALUE = -50000; // configures proper savings calculation

function tick( )
{

    var now = new Date();
    var count = parseInt((now.getTime() - START_DATE.getTime())/msInterval) 
              * INCREMENT + START_VALUE;
    var pretty = count.toFixed(2);
    if ( pretty.length > 6 ) 
    {
        pretty = pretty.substring(0,pretty.length-6) + "," + pretty.substring(pretty.length-6);
    }
    document.getElementById('counter').innerHTML = pretty;
}
tick();
setInterval( tick, INTERVAL );
</script>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 06-26-2012, 07:46 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Small error - should be

var count = parseInt((now.getTime() - START_DATE.getTime())/INTERVAL) * INCREMENT + START_VALUE;

Old Pedant's code will only insert commas up to $999,999.99. If you need bigger than that:-



Code:
<script type="text/javascript">

// works with negative numbers also
function formatNumber(x) {
return x.toFixed(2).split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
}

var num = formatNumber(1234567.8897);
num = "$" + num;
alert (num);

</script>
or add to Old Pedant's code to extend to 999999999.99

Code:
if ( pretty.length > 10 )  {
pretty = pretty.substring(0,pretty.length-10) + "," + pretty.substring(pretty.length-10);
}
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 06-26-2012 at 08:21 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
html, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:40 AM.


Advertisement
Log in to turn off these ads.