SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to calculate decimal values

    need to calculate decimal values

    example 42.925181855342466 + 58.64

    but i get this $58.64$42.93, i know js is easy on int values, but funny with floats
    any help would be great...after some quick helper code...so i can move on.
    Code:
    Esanda.prototype.calculateFornightCharge = function(payIndex,dueIndex)
    	{
    		var oThis = this;
    		alert("Pay index" +  " " + payIndex + " " + "Due index" + " " + dueIndex);
    		var pi = (payIndex-1);
    		var di = (dueIndex-1);
    		alert("Pay index" +  " " + pi + " " + "Due index" + " " + di);
    		var pay = this.oTable.rows[pi].cells[6].innerHTML;
    		var due = this.oTable.rows[di].cells[6].innerHTML;
    		alert("Due:" + oThis.dollarize(due));
    		alert("Due:" + oThis.dollarize(pay));
    		var charge = (oThis.dollarize(pay) + oThis.dollarize(due));
    		alert( charge );
    	}

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try
    Code:
    var charge = oThis.dollarize( Number( pay ) + Number( due ) );
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    precision decimal values


    Day Date Event Debit Credit Balance DailyRate
    0 Mon-8-Nov-2010 Opening 20356.6 7.33
    1 Tue-9-Nov-2010 14.66
    2 Wed-10-Nov-2010 21.990000000000002
    3 Thu-11-Nov-2010 29.32
    4 Fri-12-Nov-2010 36.65
    5 Sat-13-Nov-2010 43.980000000000004
    6 Sun-14-Nov-2010 51.31
    7 Mon-15-Nov-2010 58.64


    8 Tue-16-Nov-2010 Payment 500.00 19856.6 7.154196975890411
    9 Wed-17-Nov-2010 14.308393951780822
    10 Thu-18-Nov-2010 21.462590927671233
    11 Fri-19-Nov-2010 28.616787903561644
    12 Sat-20-Nov-2010 35.77098487945206
    13 Sun-21-Nov-2010 42.925181855342466
    14 Mon-22-Nov-2010 Due Date

    Okay why is it that when i calculate 7.33 * 8 = 58.64 and 7.15 * 6 = 42.92

    58.64 + 42.92 = 101.56
    totalcharge = 101.56
    fee = 2.31
    total = 103.87

    but i noticed on my real world statment the charge is 101.60 + fee = 103.91

    how do i get javascript to produce this 103.91 value

    i had the same problem with excel but it has a advanced feature called set precision as displayed, how can i get javascript to do it too...



    Code:
    	Esanda.prototype.calculateFornightCharge = function(payIndex,dueIndex)
    	{
    		var fee = 2.31;
    		var oThis = this;
    		var pi = (payIndex-1);
    		var di = (dueIndex-1);
    		var pay = this.oTable.rows[pi].cells[6].innerHTML;
    		var due = this.oTable.rows[di].cells[6].innerHTML;
    		var charge = (parseFloat(pay) + parseFloat(due));
    		var final = (charge + fee);
    		alert(oThis.dollarize(final));
    	}


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •