views:

41

answers:

2

Hey again everyone. Yet again i am having some problems with trying to get the match correct on this Excel Spreadsheet to JavaScript conversion.

Here is the excel formula:

 =IF(IF($B$7=TRUE,$B$28/$B$10,$B$28/$B$5)>1,1,IF($B$7=TRUE,$B$28/$B$10,$B$28/$B$5))

 WHERE
 B7  = TRUE
 B28 = 76800
 B10 = 892015
 B5  = 999500

And this is my JavaScript i have so far:

 function percent(x) { return Math.round((x-0)*100) + '%'; }

 if($('#section179').is(':checked'))
 {
  var percentRepaid = $("#rev3DScanYear").val() / $("#section179Real").val();

  if (percentRepaid > 1)
  {   
    $("#paymentCashPer").val('100.00');
  }else
  {
    percentRepaid = $("#rev3DScanYear").val() / $("#SalePrice").val();
    $("#paymentCashPer").val(percent(percentRepaid));
  }
}else
{
 //to be done   
}

 WHERE
 rev3DScanYear  = 76800
 SalePrice      = 999500
 section179Real = 892015

For the JavaScript code i keep getting a value of 8% and i should be getting a value of 8.61% as it has on the spreadsheet.

As always, any help would be great! :o)

David

+1  A: 

Math.round((x-0)*100) makes x an integer.

You could try Math.round(((x-0)*100)*100)/100 which makes the x = 8.609720... into x=861 and then divides it to get the x=8.61 you're looking for, which is what they would suggest here.

...Also, not really sure why you're subtracting 0 from x...?

Coldnorth
Coldnorth: i get 7.68 in the box when i try to do that. What am i missing??
StealthRT
For some reason on my TI-84 76800/999500 = .08 but when i do the same thing in javascript 768000/999500 it = 0.768384192096048???
StealthRT
76,800 / 999,500 = 7.68%, and 76,800 / 892,015 = 8.61%; you're dividing it by SalePrice and not section179Real.
Coldnorth
A: 

Ok, so I've been looking at this again, and I think I didn't look deeply enough the first time.

The logic, if I understand it, is this:

If Section179 is checked then Divisor is Section179Real, else it is SalePrice.

Give me the smaller of 1.00 or (rev3DScanYear / Divisor).

If that's correct, you can do it in excel with =MIN(1,$B28/IF($B$7=TRUE,$B$10,$B$5)) (same references), which means that the following should do what you want it to:

var Divisor = $("#SalePrice");

if($('#section179').is(':checked'))
{
  Divisor = $("#section179Real");
}

$("#paymentCashPer").val(Math.round(100*(Math.min(1, $("#rev3DScanYear")/Divisor)*100)/100;
Coldnorth