toFixed(2) vs Math.round

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pmactdot
    New Member
    • Dec 2007
    • 4

    toFixed(2) vs Math.round

    Hi,

    I'm trying to get the below ShowVal() case study to dispaly/return a value to 2 decimals. Where/how would I use toFixed(2) instead or Math.round?

    Code:
    function ShowVal() {
    	var I=eval(document.mortgage_calculator.interest_rate.value);
    	var N=eval(document.mortgage_calculator.total_num_months.value);
    	var S=eval(document.mortgage_calculator.loan_amount.value);
    	Document.mortgage_calculator.monthly_pymt.value=Math.round(Monthly(I,N,S)*100)/100;
    	document.mortgage_calculator.total_pymts.value=Math.round(document.mortgage_calculator.monthly_pymt.value*N*100)/100;
    }
    Cheers!
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    toFixed returns a string, Math.round returns a number.

    [CODE=javascript]

    var n=1/3;
    alert('n=1/3\nn.toFixed(2) = '+n.toFixed(2)+
    '\nMath.round(n )= '+Math.round(n) );
    [/CODE]

    Comment

    Working...