Solved

sum of numbers some of which are negative

Posted on 2013-06-19
12
814 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
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 109

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

861 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