开发者

Does anyone know of a replacement method for setFixed() that returns a number?

开发者 https://www.devze.com 2023-03-20 05:47 出处:网络
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.

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));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消