Thanks for view my blog. : )
2006/Feb/13
Thanks for view my blog. : )
2006/Feb/02
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
2006/Jan/31
I found this code in a board and I think that it will be useful for making game. Code is about add zero too number.
| Number.prototype.addZeros = function(p) { return new Array(p - length(this) + 1).join('0') + this; }; |
// Usage:
points = 200;
trace(points.addZeros(10));
or as a class:
| Score = function (points) { this.points = points; }; Score.prototype.display = function(p) { |
// Usage:
myScore = new Score(200).display(10);
trace(myScore);
Note-16-04-06 I just check for this script and it can use in old version. If you try to join by zero and trace for result, you will see "undefined" insert between zero number. So you have to remove "undefined" out.