0

Possible Duplicate:
JavaScript: formatting number with exactly two decimals

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.

This is the javascript code: http://jsfiddle.net/pqsH6/


<p style="float:left;">Money Saved: </p><b><p id="ds"  style="float:left;">$</p></b>
<div id="counter" style="float:left;"></div>

<script type="text/javascript">
    var START_DATE = new Date("january 1, 2012 12:00:00"); // put in the starting date here   
    var INTERVAL = 1000; // savings per second
    var INCREMENT = 0.005; // money saved per second
    var START_VALUE = -50000; // configures proper savings calculation
    var count = 0;
    window.onload = function()
    {
        var msInterval = INTERVAL * 1;
        var now = new Date();
        count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
        document.getElementById('counter').innerHTML = count;
        setInterval("count += INCREMENT; document.getElementById('counter').innerHTML = count;", msInterval);
    }
</script>​
Community
  • 1
  • 1
Zach
  • 3
  • 2
  • 5
  • 1
    JavaScript numbers are **floating point** and are therefore seriously ill-suited to monetary calculations. – Pointy Jun 26 '12 at 01:30
  • There's a javascript function called [ToFixed](http://www.w3schools.com/jsref/jsref_tofixed.asp) that will do what you are looking for. – The Jonas Persson Jun 26 '12 at 01:32
  • You just asked this question (and then subsequently deleted it). The answer was posted in the comments of that question. – Colin Brock Jun 26 '12 at 01:33
  • yea your right, but i am not experienced with java script and expected some one who is to take a little time out of there day to help me get this working, i stated i do not know where it goes. – Zach Jun 26 '12 at 01:38

2 Answers2

1

This looks like a way to format your output with commas using Javascript:

How to print a number with commas as thousands separators in JavaScript

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

Just pass your numbers through the function as a parameter and it will return a comma delimited number.

Here's another function you can use to round out to two decimal places:

function formatCurrency(num) {
    num = isNaN(num) || num === '' || num === null ? 0.00 : num;
    return parseFloat(num).toFixed(2);
}

Then use the function like this

var roundedCurrencyAmt = numberWithCommas(formatCurrency(amtOfMoney));

Here's a working jsFiddle: http://jsfiddle.net/alexfromapex/Y2x8m/2/

Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
  • im sorry i can not seem to get it to work, im sure i am doing it wrong =/ – Zach Jun 26 '12 at 01:37
  • i guess this problem will not get resolved, i dont know what to do with the code you sent me, i just asked for a small favor to repost the full code but i guess every one has no time for me =/ – Zach Jun 26 '12 at 01:46
  • @Zach Here is the fixed fiddle: http://jsfiddle.net/alexfromapex/Y2x8m/2/ – Alex W Jun 26 '12 at 01:47
  • OMG Alex (http://stackoverflow.com/users/1399491/alex-w), thank you so much, thankfully some one took a few seconds our of there day to help us. – Zach Jun 26 '12 at 01:56
  • It may be better to remove any existing commas before inserting more: `x.toString().replace(/,/g,'').replace(...);` – RobG Jun 26 '12 at 03:09
0

Just a comment:

> var START_DATE = new Date("january 1, 2012 12:00:00");

Relies on the built–in Date function parsing the string correctly, but ECMA-262 specifies only one format (a version of ISO8601) that Date should parse and that isn't it. But not all browsers in use support that format.

Better to use something that is both specified and known to work everywhere, e.g.:

var START_DATE = new Date(2012,0,1,12);

which will create a new date of 2012-01-01 12:00:00 in the local timezone in all browsers, or if UTC is required then:

var START_DATE = new Date(Date.UTC(2012,0,1,12));
RobG
  • 142,382
  • 31
  • 172
  • 209