1

I have a following array var total = [10, 0.25, 12.75, 1, 0];

When I do:

 for (var i= 0; i< total.length; i++) {
 totalNo += parseInt(+(total[i]));
 }

The totalNo is always a full number. Looks like the .XX after dot value is skipped. How to make sure it is added properly?

asked Aug 2, 2012 at 10:30

3 Answers 3

4

You shouldn't need to run any numeric coercion function (e.g. parseInt) - your array values are already numbers.

If an ECMA5 solution is acceptable, you can use reduce():

var arr = [10, 0.25, 12.75, 1, 0];
alert(arr.reduce(function(curr_total, val) {
 return curr_total + val;
}, 0)); //24
answered Aug 2, 2012 at 10:34
Sign up to request clarification or add additional context in comments.

10 Comments

A callback solution is way slower and could affect performances for large arrays.
@MaxArt - you're seriously voting me down for posting a native solution that does what he needs? You're sure that's a good candidate for vote-down?
you have my +1: in this specific question, the array contain only floating point values so there's no even need to use parseFloat. You correctly noted this
@MaxArt: bull****. jsperf.com/reduce-vs-native-loop. Performs 80% faster for reduce on chrome. Even if it was even or slower, I'd still prefer those methods for elegancy and convinients.
@spaceman12: I don't see the fail sorry. The original statement was "its way slower", the "slower" part implies a comparison vs. the one other answer to this question, so I'm curios who is failing here. I never stated that a native loop is not faster in general.
|
4

use parseFloat() instead of parseInt() to preserve decimal part

for (var i= 0; i< total.length; i++) {
 totalNo += parseFloat(total[i]);
}

Note1: no need to write +(total[i])
Note2: as pointed out by Utkanos, if your array values contain only floating-point values, then parseFloat is not even necessary

answered Aug 2, 2012 at 10:31

2 Comments

Or NewUser could use +total[i] instead of any parsing function. Let's remember that parseFloat("10px") === 10, and maybe that's not what we want.
+1 Good point about the unary plus operator - not necessary as parseFloat coerces to a Number.
0

You dont need either parseInt or parseFloat here. If speed is concerned and for large arrays, used the native loop, it is much faster! however care be be taken as how you code it -

var total = [10, 0.25, 12.75, 1, 0],totalNo=0;
var len=total.length;
for(var i= 0; i<len; i++) 
{
 totalNo = totalNo+total[i];
}

Always note that totalNo+=total[i] is slower than totalNo+total[i]

answered Aug 2, 2012 at 11:15

1 Comment

you can even improve that using a reverse while loop instead.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.