0

I have a mortgage calculator and I want the two outputted values to be rounded to 2 decimal places. Here is the website http://hawkstonefs.duneroot.com, as you can see it isn't rounding at all.

Below is my code, I have tried toFloat(2), Math.round and a custom function which you can see in use "roundToTwo(num)".

<div class="module">
<script>
(function (window) {

function roundToTwo(num) {    
    return +(Math.round(num + "e+2")  + "e-2");
}

var calculateMortgage = function (formId) {
var
form = document.getElementById(formId),
loan = Math.round(Number(form.loan.value.replace(',',''))),
interest_rate = form.interest_rate.value.replace(",","."),
duration, quote, total;

// refresh sanitised values
form.loan.value = loan;
form.interest_rate.value = interest_rate;

// do field validation
if (form.loan.value <= 0) {
  alert('Loan amount is required.');
  form.loan.focus();
} else if (form.duration.value == "") {
  alert('Duration is required.');
  form.duration.focus();
} else if (form.interest_rate.value == "") {
  alert('Interest rate is required.');
  form.interest_rate.focus();
} else {
if (form.duration_units.value == 1) { // Duration in years
  duration = (form.duration.value*12); // in months
} else { // Duration in months
  duration = form.duration.value; // in months
}

interest_rate = (interest_rate/12); // monthly
quote = (loan*interest_rate)/(100*(1-Math.pow((1+(interest_rate/100)), -duration)));
total = (quote * duration);

// if browser supports toFixed() method
if (Number.toFixed) {
  quote = quote.toFixed(2);
  total = total.toFixed(2);
}

form.quote.value = roundToTwo(quote); // monthly
if (form.total) {
  form.total.value = roundToTwo(total); // total
}
}
};

window.mortgageCalc = {
  calculate: calculateMortgage
};

})(window);
</script>

  <form id="mortgage_calc_form">

  <p>
  <input type="text" name="loan" id="loan" size="12" maxlength="12" placeholder="Loan amount" value="" class="inputbox" />
  </p>

  <p>
  <input type="text" name="duration" placeholder="Duration (years)" id="duration" size="2" maxlength="2" value="" class="inputbox" />
  <input type="hidden" name="duration_units" value="1" />
  </p>

  <p>
  <input type="text" name="interest_rate" id="interest_rate" placeholder="Interest rate (3.5%)" size="3" class="inputbox" />
  </p>

  <p>
  <input placeholder="Monthly repayments(GBP)" type="text" name="quote" id="quote"  size="12" class="inputbox" readonly/>
  </p>

  <p>
  <input placeholder="Total to be re-paid(GBP)" type="text" name="total" id="total"
  size="12" class="inputbox" readonly />
  </p>

  <p>
  <input class="mtBtn"  type="reset" name="reset" value="Reset" />
  <input class="mtBtn" type="submit" name="submit" value="Calculate"     onClick="mortgageCalc.calculate('mortgage_calc_form'); return false;" />
  </p>

 </form>
</div>

It has to be something on my website as this js fiddle works http://jsfiddle.net/ekhqa/

I am using Wordpress and this code is hard-coded in the sidebar.php file.

Thanks Ric

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Rik89
  • 157
  • 4
  • 22

1 Answers1

0

If you upload this plugin: http://mathjs.org/

Then you can change it to:

return math.round(num,2);

This will round the number to two decimal places.

Otherwise you could do what this guy did in this question Round to at most 2 decimal places (only if necessary)

Community
  • 1
  • 1
BCza
  • 330
  • 2
  • 13
  • [Math.round](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round) only takes one parameter – mplungjan Jul 15 '14 at 13:45