Roy Gardiner

12-08-2006, 04:17 PM

Here's two very similar solutions to a the problem of rounding numbers

<html>

<body>

<script language="JavaScript" type="text/JavaScript">

<!-- Hide code from older browsers

function fixedRound(a,n){

a= Math.round(a*Math.pow(10,n));

a/=Math.pow(10,n);

return(a.toFixed(n));

}

Number.prototype.toFixedRound= function(n){

var N= this;

N= Math.round(N*Math.pow(10,n));

N/=Math.pow(10,n);

return(N.toFixed(n));

}

var N=1.13708,n=2;

str1=N+' Rounded: '+N.toFixedRound(n);

str2=N+' Rounded: '+fixedRound(N,n);

alert("1:" + str1 + " 2:" + str2)

-->

</script>

</body>

</html> Why would you choose dot-notation over function reference, or vice versa? (I'm not really interested in whether it's a good way of rounding or not :) )

<html>

<body>

<script language="JavaScript" type="text/JavaScript">

<!-- Hide code from older browsers

function fixedRound(a,n){

a= Math.round(a*Math.pow(10,n));

a/=Math.pow(10,n);

return(a.toFixed(n));

}

Number.prototype.toFixedRound= function(n){

var N= this;

N= Math.round(N*Math.pow(10,n));

N/=Math.pow(10,n);

return(N.toFixed(n));

}

var N=1.13708,n=2;

str1=N+' Rounded: '+N.toFixedRound(n);

str2=N+' Rounded: '+fixedRound(N,n);

alert("1:" + str1 + " 2:" + str2)

-->

</script>

</body>

</html> Why would you choose dot-notation over function reference, or vice versa? (I'm not really interested in whether it's a good way of rounding or not :) )