Today someone ask me in msn that how to round digit of number for only 2 digit such as 3.15624 to be 3.17
I recommended him to multiple by 100 and round it by function Math.round() and then divide by 100 again.
So I can write function for round digit as here.
Number.prototype.roundDigit(index) { var index = Math.pow(10,index); return Math.round(this * index) / index; }; |
//usage
num = 10.1245;
trace(num.roundDigit(3)); // display 10.125
or class
Digit = function (num) { Digit.prototype.display = function(index) { return Math.round(this.num * index) / index; |
// Usage:
myDigit = new Digit(10.1245).display(3);
trace(myScore); // display 10.125