Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  134] [ 6]  / answers: 1 / hits: 6464  / 11 Years ago, sat, december 28, 2013, 12:00:00

I have problem with calcualting float values via javascript.



first my code



var d_val = 0.00;
$.each($('.prices a b'), function(index,obj){
d_val = parseFloat( $(obj).text().replace( '€', '' ) );
group_price += d_val;
});
console.log( '-----> '+ group_price );


when i run this code when adding new elements into .prices firebug prints...



-----> 0.8
-----> 1.6
-----> 2.4000000000000004
-----> 3.2


$('.prices a b') has alltime a string like 0.80€ or 0.50€
Does somebody has an idea, what the problem is?


More From » jquery

 Answers
4

Try this:



 var mycurrency = ['0.80€', '0.60€', '0.50€', '0.506€'], sum = 0;
$.each(mycurrency, function(i, obj){
var myval = parseFloat(obj.replace('€', '')).toFixed(2);
console.log('--->', myval);
sum += parseFloat(myval);
});
console.log('--->', sum, '(Sum)');



---> 0.80



---> 0.60



---> 0.50



---> 0.51



---> 2.41 (Sum)



[#49167] Friday, December 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elmer

Total Points: 432
Total Questions: 96
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
;