【问题标题】:How to write a prototype for Number.toFixed in JavaScript?如何在 JavaScript 中为 Number.toFixed 编写原型?
【发布时间】:2008-12-03 13:33:46
【问题描述】:

我需要使用 JavaScript 将十进制数字四舍五入到六位,但我需要考虑旧版浏览器,所以我 can't rely on Number.toFixed

toExponential、toFixed 和 toPrecision 的一大亮点是它们是相当现代的构造,直到 Firefox 1.5 版才在 Mozilla 中得到支持(尽管 IE 从 5.5 版开始支持这些方法)。虽然使用这些方法大多是安全的,但旧版浏览器会崩溃,因此如果您正在编写公共程序,建议您提供自己的原型,以便为旧版浏览器的这些方法提供功能。

我正在考虑使用类似的东西

Math.round(N*1000000)/1000000

向旧浏览器提供这个原型的最佳方法是什么?

【问题讨论】:

    标签: javascript rounding precision


    【解决方案1】:

    试试这个:

    if (!Number.prototype.toFixed)
    
        Number.prototype.toFixed = function(precision) {
            var power = Math.pow(10, precision || 0);
            return String(Math.round(this * power)/power);
        }
    

    【讨论】:

    • 虽然有些人可能不认为这是 javascript toFixed shim 或 shiv,但在此页面上出现这些单词会使我的谷歌搜索快 10 秒;)
    【解决方案2】:

    我认为 Firefox 1.5 和 IE 5 几乎不再使用,或者被极少数人使用。
    有点像支持 Netscape Navigator 的编码... :-)
    除非其他主要浏览器(Opera?Safari?不太可能......)不支持此功能,或者如果您的 Web 日志显示大量使用旧版浏览器,您可能只使用这些方法。
    有时,我们必须继续前进。 ^_^

    [编辑] 在 Opera 9.50 和 Safari 3.1 上运行良好

    javascript: var num = 3.1415926535897932384; alert(num.toFixed(7));
    

    你参考的那篇文章是一年半以前的,IT行业的永恒……我觉得和IE用户不同,火狐用户经常去最新版本。

    【讨论】:

    • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
    • @PatrickKostjens 你知道你在批评 5 年前的回应吗? (“IT 行业的永恒”,引用我自己的话......)如果您打算批评 SO 中的所有这些类型的响应,那么您还有一些工作要做! :-) 此外,从某种意义上说,这是一个真正的答案,OP 不应该关心在提出问题时已经过时的浏览器......即。他的问题可能毫无意义。
    • 我没有意识到这一点。我同意这是一个很长的时间。这篇文章在我的评论队列中。我认为它被某人标记为低质量,否则我不知道为什么它会出现以供审核。
    【解决方案3】:

    来自Bytes website,这个功能和Serge llinsky的差不多:

    if (!num.toFixed) 
    {
      Number.prototype.toFixed = function(precision) 
      {
         var num = (Math.round(this*Math.pow(10,precision))).toString();
         return num.substring(0,num.length-precision) + "." + 
                num.substring(num.length-precision, num.length);
      }
    }
    

    【讨论】:

      【解决方案4】:

      另一个选项是 ( 它不会不必要地转换为字符串,并且还纠正了 (162.295).toFixed(2) 到 162.29 的错误计算(应该是 162.30 ))。

      Number.prototype._toFixed=Number.prototype.toFixed; //Preserves the current function
      Number.prototype.toFixed=function(precision){
      /* step 1 */ var a=this, pre=Math.pow(10,precision||0);
      /* step 2 */ a*=pre; //currently number is 162295.499999
      /* step 3 */ a = a._toFixed(2); //sets 2 more digits of precision creating 16230.00
      /* step 4 */ a = Math.round(a);
      /* step 5 */ a/=pre;
      /* step 6 */ return a._toFixed(precision);
      }
      /*This last step corrects the number of digits from 162.3 ( which is what we get in
      step 5 to the corrected 162.30. Without it we would get 162.3 */

      编辑:在尝试这个特定的化身时,this*=Math.pow(10, precision||0) 创建一个错误无效的左手赋值。所以给这个关键字变量a。如果我关闭我的函数也会有所帮助^_^;;

      【讨论】:

        【解决方案5】:

        试试这个:

         Number.prototype.toFixed = function(precision) {
             var power = Math.pow(10, precision || 0);
             return String(Math.round(this * power)/power);
         }
        

        【讨论】:

          猜你喜欢
          • 2018-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-03
          • 1970-01-01
          相关资源
          最近更新 更多