Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sum of numbers some of which are negative

Posted on 2013-06-19
12
Medium Priority
?
909 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:Meir Rivkin
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:Meir Rivkin
ID: 39259293
try using parseFloat instead of parseInt
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

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

Expert Comment

by:Meir Rivkin
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:Meir Rivkin
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 111

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:
Meir Rivkin earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

598 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