Working wi开发者_StackOverflow中文版th the method setFixed() returns a string in most browsers, but sometimes I need to set a number to a fixed set of decimal places and I need it to stay a number.
Plus I would like to be able to access it from the number object in javascript as part of the prototype object.
And it should ALWAYS return a number as a result instead of a string.
Anyone know of a good article or method to use?
I would also like a way to control whether it rounds up, down, or uses standard rounding.
Yes, there is an article on doing just that here:
http://exoboy.wordpress.com/2011/07/14/the-problems-with-tofixed-in-javascript/
Number.prototype.decimals=function(n){
return +(this.toFixed(n));
}
精彩评论