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
Related
I want to grab all the values from the label with class price and add them using jQuery. Now actual webpage is different but to understand the concept I am only putting minimum code here. There are 3 labels for prices, and 1 more for total:
<label class="price">120</label>
<label class="price">250</label>
<label class="price">342</label>
<label id="total"></label>
I read that .each() can be used but I could not understand how to use it for this purpose.
I have uploaded jsfiddle over here http://jsfiddle.net/vivpad/cysjtrh8/1/
Basic example
jQuery(document).ready(function($){
var total = 0;
$('label.price').each(function(){
var value = parseFloat($(this).text());
total += value;
});
$('#total').text(total);
});
DEMO
You could get total price mapping .price elements text:
jQuery(document).ready(function ($) {
$('#total').text(function () {
return $('.price').map(function () {
return +$(this).text()
}).get().reduce(function (pv, cv) {
return pv + cv;
}, 0);
});
});
-jsFiddle-
Note that you need to add jquery to your jsfiddle.
Also - you don't need to use .each - you can use arrays as well. Which simplifies it much and it is more efficient. See here: http://jsfiddle.net/vivpad/cysjtrh8/9
var sum = 0;
var prices = $("label.price");
for (var i = 0; i < prices.length; i++)
sum += parseInt($(prices[i]).text());
$("#total").text(sum);
try this:
jQuery(document).ready(function($){
var total = 0;
$('.price').each(function() {
var temp = $(this).html();
total += parseFloat(temp);
});
$('#total').html(total);
});
JsFiddle
Try:
var labelvalues = $('label').map(function () {
return $(this).text();
}).get();
var total = 0;
for (var i = 0; i < labelvalues.length; i++) {
total += labelvalues[i] << 0;
}
$("#total").text(total);
DEMO
Here I am using map function to translate all items to new array of items. And add the elements in the array.
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"
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);
});
Hi I am trying to sum an array on Javascript with the following codes.
var data[]:
var total=0;
data.push[x]; // x is numbers which are produced dynamically.
for(var i=0, n=data.length; i < n; i++)
{
total=total+data[i];
}
alert(total)
for example if x values are are respectively 5,11,16,7. It shows the total value as 511167 not sum the values 5+11+16+7=39
Do you have any idea why it results like that?
Thanks.
Use parseInt() function javascript
total=parseInt(total)+parseInt(data[i]);
Try with parseInt:
total=total+parseInt(data[i]);
Simply whip a unary + before data[i] to convert the string values to numeric values:
total = total + (+data[i]);
Even better, use += instead of total=total+...:
total += +data[i];
JSFiddle demo.
Try this one:
var total = 0;
for (var i = 0; i < someArray.length; i++) {
total += someArray[i] << 0;
}
Use parseInt() javascript function....
total = total + parseInt(data[i]);
This looks the 'x' which you mention come dynamically has a string type. Just check the "typeof x".
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