Reputation: 4609
This is my javascript code :
var dsum = 0.0;
$.each($("#Report tbody tr"), function () {
var x = 0.0;
if ($(this).find("td:eq(3) div").html() != " ")
x = parseFloat($(this).find("td:eq(3) div").html().replace(/,/g, ""));
dsum += x;
});
var csum = 0.0;
$.each($("#Report tbody tr"), function () {
var x = 0.0;
if ($(this).find("td:eq(4) div").html() != " ")
x = parseFloat($(this).find("td:eq(4) div").html().replace(/,/g, ""));
csum += x;
});
var lastEvenRow=$("#Report").find("tr:last").hasClass('erow');
$("<tr id='LastRow'><td colspan='3' style='border-top: 1px solid #DDDDDD;'><div style='padding:5px;text-align:right;'>Total :</div></td><td style='text-align:center;height:22px;border-top: 1px solid #DDDDDD;'><div style='padding:5px;'>" + dsum.toString(10).replace( /(\d)(?=(\d\d\d)+(?!\d))/g , "$1,") + "</div></td><td style='text-align:center;border-top: 1px solid #DDDDDD;'><div style='padding:5px;'>" + csum.toString(10).replace( /(\d)(?=(\d\d\d)+(?!\d))/g , "$1,") + "</div></td><td style='text-align:center;border-top: 0px solid #DDDDDD;'><div style='padding:5px;border-top: 1px solid #DDDDDD;'>" + (dsum > csum ? (dsum - csum).toString(10).replace( /(\d)(?=(\d\d\d)+(?!\d))/g , "$1,") : " ") + "</div></td><td style='text-align:center;'><div style='padding:5px;'>" + (csum >= dsum ? (csum - dsum).toString(10).replace( /(\d)(?=(\d\d\d)+(?!\d))/g , "$1,") : " ") + "</div></td></tr>").appendTo("#Report tbody");
if(lastEvenRow==false)
$("#LastRow").addClass("erow");
As I mentioned in the picture , i think the result must be something like 104,889.01 but , as you see , It is Incorrect value
Is there any body out there to help me ? Sorry about my bad English syntax(I am new in English)
UPDATE1 :
1318114.01 - 1213225 must be 104889.01 , so why javascript return 104889.01000000001?!!
Upvotes: 0
Views: 1566
Reputation: 11352
It happens because it works with convertions from binary to decimal, then we have aproximations, whose result on big floating numbers. See:
var i = 1;//1
i += .1;//1.1
i += .1;//1.2000000000000002
You can try an own aproximation:
var result = Math.round((1318114.01-1213225)*1000)/1000;
Upvotes: 2