Solved

sum of numbers some of which are negative

Posted on 2013-06-19
12
840 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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 110

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

687 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