I have a variable count set to zero.
var count = 0;
Then I run through a .each() loop to update the count. The .text() parameter will always return a text number.
$(this).find('.outersubtopic').each(function(idx)
{
var temp = $(this).text();
count += temp;
});
alert(count);
When I hit the alert, it always reads 0008 instead of just 8.
How do I turn the .text() into a real number?
$(this).find('.outersubtopic').each(function(idx)
{
var temp = $(this).text();
count += window.parseInt(temp);
});
alert(count);
window.parseInt() should do it for you.
you have to convert it to an Number
$(this).find('.outersubtopic').each(function(idx)
{
var temp = $(this).text();
count += Number(temp);
});
alert(count);
try using parseInt
$(this).find('.outersubtopic').each(function(idx)
{
var temp = $(this).text();
count += temp;
});
alert(parseInt(count));
count += 1 * temp; // quick way to change string to number
Related
Im trying to get the sum of a column ("price") with angular with this code, but only have the value.
Example:
Price: 5,7,8,9
Total Price:05789
$scope.totalPrice = function(){
var total = 0;
for(count=0;count<$scope.names.length;count++){
var product = $scope.names[count];
total += (product.price);
}
return total;
};
It looks like the product.price is str, so each time you use +=, you are concatenating the string.
Try using parseFloat or parseInt
$scope.totalPrice = function(){
var total = 0;
for(count=0;count<$scope.names.length;count++){
var product = $scope.names[count];
total += parseFloat(product.price);
}
return total;
};
EDIT 1:: Double checking it, you declare var total = 0 and that is a Int, the += between a Int and a str should give a Int... weird stuff...
EDIT 2: Well, triple checking it... the first thing I said was ok :D 0 + "0" gives "00"
I have a table of fines people owe:
<tr><td class='fines'>104.20</td></tr>
<tr><td class='fines'>26.40</td></tr>
<tr><td class='fines'>99.14</td></tr>
I am trying to get the sum of the cells:
var sumFines = "";
$.each($('.fines'),function(){
alert($(this).html());
sumFines += parseFloat($(this).html());
})
alert(sumFines);
The result of each alert in each loop is the number in the cell. The result of the final alert is "NaN", Why?
When you sum
"" + 120
the result return a string, you must sum number, so change
var sumFines = "";
to
var sumFines = 0;
I need to set initial value of sumFines to 0.
var sumFines = 0;
$.each($('.fines'),function(){
alert($(this).html());
sumFines += parseFloat($(this).html());
})
alert(sumFines);
var sumFines = ""; makes sumFines a string and that's the reason you get NaN because you are doin arithmatic operations with a string and float which can't be cast together.
If you want to keep the .each approach, just change your initial value to 0.
var subtotal = 0;
$('.fines').each(function() {
subtotal += !isNaN(+$(this).html()) && +$(this).html();
});
alert(subtotal);
The line:
!isNaN(+$(this).html()) && +$(this).html();
asserts that you are effectively adding a number, else nothing.
fiddle
http://jsfiddle.net/hem7q4fn/2/
Nan reduce whole expression to NaN
try:
var sumFines = 0;
$('.fines').each(function () {
var num = parseFloat($(this).txt());
sumFines += isNaN(num) ? 0 : num;
});
alert(sumFines);
This calculation script works almost, but it doesn't do anything with a value with a number behind the comma. Can anyone help me?
var selects = $('select');
var inputs = $('input');
selects.change(calculate);
inputs.keyup(calculate);
function calculate()
{
var runningTotal = 0;
selects.each(function(i)
{
var val = parseInt($(this).val());
var qty = inputs.eq(i).val();
runningTotal += (val*qty);
});
$('#grandtotal').val(runningTotal);
}
If you're trying to convert a string to a number with a decimal point, don't use parseInt(), but parseFloat() instead.
jsfiddle: http://jsfiddle.net/Grimbode/mjs32/1/
alert(parseInt('10.10'));
alert(parseFloat('10,10'));
alert(parseFloat('10.10'));
You need to convert you commas to '.'s apparantely for the parseFloat() to work.
parseFloat(('10,10').replace(",", "."));
selects.each(function(i)
{
var val = parseInt($(this).val());
if (val.indexOf('"') != -1) {
val = val.replace(/"/g, '');
}
var qty = inputs.eq(i).val();
runningTotal += (val*qty);
});
So I have a string called hoursString that looks like
4.3|2.4|3.4|3.0|2.64|3.0|1.0|0.0|2.6|3.4|
I split the string into an array and what I am trying to do is add up all the numbers and return it to the html page in the div "test".
JAVASCRIPT:
var hours=hoursString.split("|");
var total=0;
for (var i=0;i<hours.length;i++)
{
total += hours[i];
}
document.getElementById("test").innerHTML=total;
The result is NaN. Can someone tell me what I am doing wrong?
Thanks!
4.3|2.4|3.4|3.0|2.64|3.0|1.0|0.0|2.6|3.4|
remove the last |, split will otherwise add an empty string at the end of the array which will give NaN and NaN + number evaluates to NaN
edit: and yes, parseFloat is necessary too, otherwise the result will be a string instead of NaN
var hoursString = "4.3|2.4|3.4|3.0|2.64|3.0|1.0|0.0|2.6|3.4|",
hours = hoursString.split("|"),
total = 0;
for (var i = 0; i < hours.length; i++) {
total += parseFloat(hours[i]) || 0; // see explanation
}
document.getElementById("test").innerHTML = total;
Why the || 0? Because it checks if the string is "", if so the || operand will return the second value, in this case 0.
This will output 25.740000000000002. See this answer on the cause of the "wrong" result, and here is a working fiddle.
Your issue is that each element in hours is still a string value even though it is written as a number. The easiest way to fix this would be to use javascripts parseFloat() function.
Example:
var hours=hoursString.split("|");
var total=0;
for (var i=0;i<hours.length;i++)
{
total += parseFloat(hours[i]);
}
document.getElementById("test").innerHTML=total;
EDIT: Changed from parseInt to parseFloat
var hoursString = "4.3|2.4|3.4|3.0|2.64|3.0|1.0|0.0|2.6|3.4|"
var hours=hoursString.split("|");
var total=0;
var idx = 0;
for(idx in hours) {
total += +hours[idx];
}
alert(total);
FIDDLE
I need to sum some money value, i have this html
<div class=''>54,44</div>
<div class=''>34,10</div>
<div class=''>44,00</div>
<div class=''>14,50</div>
How can i make an alert with the sum of these div text?? ( the right sum!! ;-) )
Many thanks!
Add some class like money (or use $("div") selector) and do something like this:
var sum = 0;
$(".money").each(function(){
sum += parseFloat($(this).text().replace(",", "."));
});
alert(sum);
Firstly, its worthwhile putting a data-attribute on each div with the raw amount, devoid of any locale-specific formatting.
eg/
<div data-amount="55.44">54,44</div>
Then assuming these div's are identifiable you can simply do
var total = 0;
$('some-selector-for-your-divs').each(function(){
total += parseFloat($(this).data('amount'));
});
Edit: Duh! parseInt on monetery amounts fixed.
A comma isn't a legal decimal separator in Javascript, so you'll have to convert the commas into decimal points:
var sum = 0;
$('div').each(function() {
var v = $(this).text().replace(',', '.');
sum += parseFloat(v);
});
alert(sum);
var sum = 0;
$('div').each(function() {
sum += $(this).text();
});
alert("The sum is "+ sum);
Use following code
var sum=0;
$('div').each(function(){
sum+=parseFloat($(this).text().replace(',','.');
});
var sum = 0;
$('div').each(function() {
var val = $(this).text().replace(',', '.'); // Replace , with .
sum += parseFloat(val);
});
(demo)
Update
The base 10 thing was crap.
var sum=0;
$('div').each(
function(){
sum+=Number($(this).html());
}
);
alert(sum );
Don't forget convert numbers to coreect format