Javascript小技巧,去掉小数位并且不会四舍五入
来源:广州中睿信息技术有限公司官网
发布时间:2012/10/21 23:25:16 编辑:admin 阅读 672
1:varn3=52.36852:document.writeln(n3>>0)//523:可以去掉小数。.csharpcode,.csharpcodepre{font-size:small
   1:  var n3 = 52.3685;
   2:  document.writeln(n3 >> 0);// 52
   3:  可以去掉小数。
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

如上代码,就是这么简单

右移位操作导致小数部分丢失,为何会这样呢?左移位可以吗?移位操作是否都有如此功能呢?

带着疑问又写了一段代码用来测试以上想法,继续上代码

   1:  {
   2:      n = 52.123456;
   3:      //alert(typeof n);
   4:      alert(n);
   5:  }
   6:  //有符号右移
   7:  {
   8:      n = 52.123456;
   9:      var n2 = n >> 0;
  10:      //alert(typeof n2);
  11:      alert(n2);
  12:  }
  13:  //无符号右移
  14:  {
  15:      n = 52.123456;
  16:      var n3 = n >>> 0;
  17:      //alert(typeof n3);
  18:      alert(n3);
  19:  }
  20:  //左移0位
  21:  {
  22:      n = 52.123456;
  23:      var n4 = n << 0;
  24:      //alert(typeof n4);
  25:      alert(n4);
  26:  }
  27:  //按位或or
  28:  {
  29:      n = 52.123456;
  30:      var n5 = n | 0;
  31:      //alert(typeof n5);
  32:      alert(n5);
  33:  }
  34:  //按位异或xor
  35:  {
  36:      n = 52.123456;
  37:      var n6 = n ^ 0;
  38:      //alert(typeof n6);
  39:      alert(n6);
  40:  }
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

那,这里不卖关子,直接给出测试结果来:以上五种方法均可以去掉小数点;然而为什么会这样呢?
翻翻EAMCScript规范吧,或许里边会有答案,见http://bclary.com/2004/11/07/#a-9.5

 

先来看看位操作都做了什么,下边是位操作的实现步骤,重点在第五,第六步

11.7.1 The Left Shift Operator (<< )

Performs a bitwise left shift operation on the left operand by the amount specified by the right operand.

The production ShiftExpression : ShiftExpression << AdditiveExpression is evaluated as follows:

1. Evaluate ShiftExpression.

2.Call GetValue(Result(1)).

3.Evaluate AdditiveExpression.

4. Call GetValue(Result(3)).

5.Call ToInt32(Result(2)).

6.Call ToUint32(Result(4)).

7.Mask out all but the least significant 5 bits of Result(6), that is, compute Result(6) & 0x1F.

8.Left shift Result(5) by Result(7) bits. The result is a signed 32 bit integer.

9.Return Result(8).|

再来看看那锅ToInt32干了什么,重点在第三步
9.5 ToInt32: (Signed 32 Bit Integer)

The operator ToInt32 converts its argument to one of 232 integer values in the range -231 through 231-1, inclusive. This operator functions as follows:

1. Call ToNumber on the input argument.

2. If Result(1) is NaN, +0, -0, +∞, or -∞, return +0.

3. Compute sign(Result(1)) * floor(abs(Result(1))).

4. Compute Result(3) modulo 232 ; that is, a finite integer value k of Number type with positive sign and less than 232 in magnitude such the mathematical difference of Result(3) and k is mathematically an integer multiple of 232 .

5. If Result(4) is greater than or equal to 231 , return Result(4)-232 , otherwise return Result(4).

 

最后来看那个floor是什么意思,这里重点看第三步的后半拉,就是那个floor是干什么滴

floor(x) = x-(x modulo 1)

看见没,就在这一步把小数干掉了

Floor(x) 等于x减去x模上1

N= 52.123456 – 52.123456%1

=52.123456-0.1234559999999

=52

 

搜代斯呐,春节快乐~


 

 
 
 
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }


------------------------------------------
除非特别声明,文章均为原创,版权与博客园共有,转载请保留出处
联系我们CONTACT 扫一扫
愿景:成为最专业的软件研发服务领航者
中睿信息技术有限公司 广州•深圳 Tel:020-38931912 务实 Pragmatic
广州:广州市天河区翰景路1号金星大厦18层中睿信息 Fax:020-38931912 专业 Professional
深圳:深圳市福田区车公庙有色金属大厦509~510 Tel:0755-25855012 诚信 Integrity
所有权声明:PMI, PMP, Project Management Professional, PMI-ACP, PMI-PBA和PMBOK是项目管理协会(Project Management Institute, Inc.)的注册标志。
版权所有:广州中睿信息技术有限公司 粤ICP备13082838号-2