Solved

# how to accumulate a decimal total in jQuery / Javascript

Posted on 2013-02-04
710 Views
`````` \$('span[id*="lblAmt"]').each(function () {
\$(this).css(
'border', 'solid 1px green'
);

tot += \$(this).text();
});

``````

I've established that it loops the appropriate number of times inside the each( ).

But the total in the ALERT is wacky.

Do I need to convert the text( ) to a decimal or something or other?

How can I accumlate values like this inside the each:

415.66
434
320.17
.17

Thank you.

0
Question by:knowlton
• 3
• 2

LVL 82

Accepted Solution

leakim971 earned 500 total points
``````var tot = 0;
\$('span[id*="lblAmt"]').each(function () {
\$(this).css(
'border', 'solid 1px green'
);

tot += \$(this).text() * 1; // or tot += parseFloat( \$(this).text() )
});

``````
0

LVL 5

Author Comment

Yeah...it was parseFloat that I needed, I think!

``````  \$('span[id*="lblAmt"]').each(function () {
tot += parseFloat(\$(this).text());
});
``````

0

LVL 5

Author Closing Comment

Thank you.
0

LVL 18

Expert Comment

Here is a test script that works with your example data of strings.

Essentially we use a variety of built in Javascript functions and we are sure to initialize tot to 0:
parseFloat: http://www.w3schools.com/jsref/jsref_parsefloat.asp
Math.Round: http://www.w3schools.com/jsref/jsref_round.asp

``````<script type="text/javascript">
//initialize tot to 0
var tot = 0;
// array of string values
var values=new Array("415.66","434","320.17",".17");
//loop through with parseFloat
for (var i=0;i<values.length;i++)
{
tot += parseFloat(values[i]);
}
// round to two decimal places
tot = Math.round(tot*100)/100;
//output tot, use .toFixed(2) if you want two decimal places to always show
</script>
``````

``````//initialize
var tot = 0;

\$('span[id*="lblAmt"]').each(function () {
\$(this).css(
'border', 'solid 1px green'
);
//parseFloat
tot += parseFloat(\$(this).text());
});

// round to two decimal places, use 1000 for 3 decimal places, etc. You must do this because of float values extreme decimal places.
tot = Math.round(tot*100)/100;

//output
``````
0

LVL 82

Expert Comment

longer than * 1 but more explicit too...
0

LVL 5

Author Comment

matthewstevenkelly:

Thank you for this additional info!

Tom
0

## Featured Post

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…