Solved

sum of numbers some of which are negative

Posted on 2013-06-19
12
777 Views
Last Modified: 2013-06-19
I an trying to run a bunch of input fields with numbers through jquery to get their sum but it does not seem to work with negative numbers. If I put a negative number in the first input field I get "NaN".

Basically my script is upon keyup (changing a value in an input), it will automatically pull all the numbers from the same column and drop in the total a the bottom.

Why am I having problems getting a sum of numbers with negatives?

$.fn.sumValues = function() {
	var sum = 0; 
	this.each(function() {
		sum += Number($(this).val());
	});
	return sum;
};

Open in new window

0
Comment
Question by:the-miz
12 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39259264
try parseInt:
$.fn.sumValues = function() {
	var sum = 0; 
	this.each(function() {
var val = $(this).val();
if(val != ""){
		sum += parseInt(val);
}
	});
	return sum;
};

Open in new window

0
 

Author Comment

by:the-miz
ID: 39259287
Guess I should have mentioned I tried parseInt but it does the same thing.  I am working with currency values which may include cents. The number that is being pushed through the jquery looks like 1234.12 (for example) or -1234.12 (for a negative example).  It either comes out as 0 or NaN
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39259293
try using parseFloat instead of parseInt
0
 

Author Comment

by:the-miz
ID: 39259297
parseFloat returns NaN
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39259305
what is the value passed to parseFloat which returns NaN?
parseFloat works with both negative/positive decimal numbers
0
 

Author Comment

by:the-miz
ID: 39259314
Sorry sedgwick, turns out its actually passing the full currency value including $ and commas

Should of caught that.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 42

Expert Comment

by:sedgwick
ID: 39259322
awesome, do u need any further help?
do u know how to get rid of the $ sign?
0
 

Author Comment

by:the-miz
ID: 39259329
The number is being passed as $1,234.12 so I need it to be 1234.12 (or negative depending on the value).  Also, do you still recommend using parseFloat?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39259335
On the outside chance that PHP is involved here, you can sanitize the numbers with a regular expression to remove the non-numeric elements (everything that is not a number, a dot or a dash). Something like this...

$money = '$2.54';
$numbr = preg_replace('/[0-9\.\-]/', NULL, $money);
var_dump($money, $numbr);

Open in new window

0
 

Author Comment

by:the-miz
ID: 39259344
Does this look good?

$.fn.sumValues = function() {
	var sum = 0; 
	var pattern         = /[^0-9.-]+/g;
	this.each(function() {
		var num = $(this).val();
		sum += parseFloat(num.replace(pattern,''));
	});
	return sum;
};

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39259357
use remove + regext to remove $ sign and the comma:
$.fn.sumValues = function() {
	var sum = 0; 
	this.each(function() {
var val = $(this).val();
if(val != ""){
		sum += parseFloat(val.replace(/[$,]/g,''));
}
	});
	return sum;
};

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 39260312
Try this :
$.fn.sumValues = function() {
	var sum = 0; 
	this.each(function() {
		sum += $(this).val() * 1;
	});
	return sum;
};

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now