Possible Duplicate:
How is floating point stored? When does it matter?
Here is an example with 3 options and 2 different problems. http://jsfiddle.net/pEpFA/7/
- option 1 - ground / displays properly.
- option 2 - express / omits the '0' from the value
- option 3 - overnight / add's
000000000000000
to the total.
How do i get this to work properly? I want the outcome to be displayed like option 1. Is this a javaScript bug?
HTML
<label><input type="radio" name="print" class="option" data-number="25.72" /> UPS Ground </label>
<br>
<label><input type="radio" name="print" class="option" data-number="80.90" /> UPS Express </label>
<br>
<label><input type="radio" name="print" class="option" data-number="112.93" /> UPS Overnight </label>
<br><br>
Shipping
$<span id="ship_total"></span>
<br>
Final Total
$<span id="new_total"></span>
jQuery
$(document).ready(function() {
$('label').click(function() {
var total = 0;
$('.option:checked').each(function() {
total += Number($(this).data('number'));
});
$('#ship_total').text(total);
//php echos the subtotal
var sub_total = 550.25;
var ship_total = ($("#ship_total").text());
var final_total = parseFloat(sub_total) + parseFloat(ship_total);
$('#new_total').text(final_total);
});
});
See jsfiddle DEMO: http://jsfiddle.net/pEpFA/7/