Tuesday, August 31, 2010

Rounding a number to decimal places, Javascript.

As Javascript's Math.Round() cant be directly used to round to decimal places, we should find the workarounds.
Have a look at the following function which does the job.

function RoundNumber(ThisNumber, rlength) { // Arguments: number to round, number of decimal places
  var RoundedNumber = Math.round(ThisNumber*Math.pow(10,rlength))/Math.pow(10,rlength);
  return RoundedNumber ; // returns the resulting number. 
}

No comments: