改写toFixed方法

<html>
  <head>
    <script type="text/javascript">
      Number.prototype.toFixed = function(d) {
        var s = this + "";
        if (!d) d = 0;
        if (s.indexOf(".") == -1) s += ".";
        s += new Array(d + 1).join("0");
        if (
          new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)
        ) {
          var s = "0" + RegExp.$2,
            pm = RegExp.$1,
            a = RegExp.$3.length,
            b = true;
          if (a == d + 2) {
            a = s.match(/\d/g);
            if (parseInt(a[a.length - 1]) > 4) {
              for (var i = a.length - 2; i >= 0; i--) {
                a[i] = parseInt(a[i]) + 1;
                if (a[i] == 10) {
                  a[i] = 0;
                  b = i != 1;
                } else break;
              }
            }
            s = a
              .join("")
              .replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2");
          }
          if (b) s = s.substr(1);
          return (pm + s).replace(/\.$/, "");
        }
        return this + "";
      };
    </script>
  </head>
  <body>
    <input
      type="button"
      value="显示0.009.toFixed(2)"
      onclick="alert(0.009.toFixed(2))"
    /><br />
    <input
      type="button"
      value="显示0.123.toFixed(2)"
      onclick="alert(0.123.toFixed(2))"
    /><br />
    <input
      type="button"
      value="显示0.125.toFixed(2)"
      onclick="alert(0.125.toFixed(2))"
    /><br />
    <input
      type="button"
      value="显示0.126.toFixed(2)"
      onclick="alert(0.126.toFixed(2))"
    /><br />
    <input
      type="button"
      value="显示20.445.toFixed(2)"
      onclick="alert(20.445.toFixed(2))"
    /><br />
    <input
      onclick="alert(20.405.toFixed(2))"
      type="button"
      value="显示20.405.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(20.415.toFixed(2))"
      type="button"
      value="显示20.415.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(20.425.toFixed(2))"
      type="button"
      value="显示20.425.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(20.435.toFixed(2))"
      type="button"
      value="显示20.435.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(20.445.toFixed(2))"
      type="button"
      value="显示20.445.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(20.455.toFixed(2))"
      type="button"
      value="显示20.455.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(20.465.toFixed(2))"
      type="button"
      value="显示20.465.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(20.475.toFixed(2))"
      type="button"
      value="显示20.475.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(20.485.toFixed(2))"
      type="button"
      value="显示20.485.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(20.495.toFixed(2))"
      type="button"
      value="显示20.495.toFixed(2)"
    />
    <br />
    <input
      onclick="alert(0.05.toFixed(1))"
      type="button"
      value="显示0.05.toFixed(1)"
    />
    <br />
    <input
      onclick="alert(0.15.toFixed(1))"
      type="button"
      value="显示0.15.toFixed(1)"
    />
    <br />
    <input
      onclick="alert(0.25.toFixed(1))"
      type="button"
      value="显示0.25.toFixed(1)"
    />
    <br />
    <input
      onclick="alert(0.35.toFixed(1))"
      type="button"
      value="显示0.35.toFixed(1)"
    />
    <br />
    <input
      onclick="alert(0.45.toFixed(1))"
      type="button"
      value="显示0.45.toFixed(1)"
    />
    <br />
    <input
      onclick="alert(0.55.toFixed(1))"
      type="button"
      value="显示0.55.toFixed(1)"
    />
    <br />
    <input
      onclick="alert(0.65.toFixed(1))"
      type="button"
      value="显示0.65.toFixed(1)"
    />
    <br />
    <input
      onclick="alert(0.75.toFixed(1))"
      type="button"
      value="显示0.75.toFixed(1)"
    />
    <br />
    <input
      onclick="alert(0.85.toFixed(1))"
      type="button"
      value="显示0.85.toFixed(1)"
    />
    <br />
    <input
      onclick="alert(0.95.toFixed(1))"
      type="button"
      value="显示0.95.toFixed(1)"
    />
    <br />
  </body>
</html>

原文链接:https://www.cnblogs.com/wangsaiming/p/4644790.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值