为什么 parseInt(0.0000008) === 8?

原文:http://justjavac.com/javascript/2015/01/08/why-parseint-0-00000008-euqal-8-in-js.html

IEEE 754

JavaScript 的数字系统是采用 IEEE 754,一开始看到这个问题,以为是 IEEE 754 导致的问题。

常见的问题有浮点数比较:

console.log((0.1 + 0.2) == 0.3);  // false
console.log((0.1 + 0.2) === 0.3); // false
console.log(0.1 + 0.2); // 0.30000000000000004

后来发现这问题并不会导致 parseInt(0.0000008) 变成 8,那么问题就可能在 parseInt 这个函数上。

## parseInt
> `parseInt(string, radix)`

parseInt 接受两个参数,第一个参数是要转换的字符串(忽略空白);第二个参数是基数。

例如:

parseInt('   12', 10);  // 12
parseInt('12**', 10);   // 12
parseInt('12.34', 10);  // 12
parseInt(12.34, 10);    // 12

最后一个例子让我们看到 parseInt 可以将数字类型转换成整数,但最好别这么做。

再来看下面这个例子:

parseInt(1000000000000000000000.5, 10); // 1

为什么会这样呢?

parseInt 的第一个类型是字符串,所以会将传入的参数转换成字符串,也就是String(1000000000000000000000.5) 的结果为 '1e+21'parseInt 并没有将 'e' 视为一个数字,所以在转换到 1 后就停止了。

这也就可以解释 parseInt(0.0000008) === 8

String(0.000008);  // '0.000008'
String(0.0000008); // '8e-7'

从上面的程式码可以看出,小于 0.0000001(1e-7) 的数字转换成 String 时,会变成科学记号法,再对这个数进行 parseInt 操作就会导致这个问题发生。

结论

不要将 parseInt 当做转换 Number 和 Integer 的工具。

再补上一些悲剧:

parseInt(1/0, 19);      // 18
parseInt(false, 16);    // 250
parseInt(parseInt, 16); // 15
parseInt("0x10");       // 16
parseInt("10", 2);      // 2

参考资料


----

来自自己的后记:看完之后,顿觉茅塞顿开

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
function webAudio(options){ //public this.volume = parseInt(options.volume || 100) ; // 音量控制单元,值为Number类型,范围为0-100 this.url = options.url || '' ; // 音频资源url,值类型为String类型 this.autoPlay = !!options.autoPlay; // 是否加载完成自动播放,值类型为Boolean类型 this.loopPlay = !!options.loopPlay; // 是否循环播放,值类型为Boolean类型 //private this.buffer = null; this.context = null; this.sourceAudio = null; this.gainNode = null; this.loadReady = false; //初始化 this.init = function () { window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext; if(!AudioContext){ console.error("您的浏览器不支持HTML5 audio API"); return } this.context = new AudioContext(); this.loadResource(); } //下载音频资源 this.loadResource = function () { var _this = this; var xhr = new XMLHttpRequest(); xhr.open('GET',this.url,true); xhr.responseType = 'arraybuffer'; xhr.onload = function () { _this.context.decodeAudioData(xhr.response,function (buffer) { _this.buffer = buffer; _this.prepareAudio(); this.loadReady = true; }) } xhr.send(); } //是否自动播放 this.prepareAudio = function () { this.autoPlay ? this.startAudio() : ''; } //创建音频 this.createAudio = function () { this.sourceAudio = this.context.createBufferSource();//创建一个音频源 相当于是装音频的容器 this.sourceAudio.buffer = this.buffer;// 告诉音频源 播放哪一段音频 this.gainNode = this.context.createGain();//调节音量 this.sourceAudio.connect(this.gainNode); this.changeVolume();//声音 this.sourceAudio.loop = this.loopPlay;//循环 this.gainNode.connect(this.context.destination);// 连接到输出源 } //重新播放 this.startAudio = function () { this.createAudio(); this.sourceAudio.start(0);//开始播放 } //改变音量 this.changeVolume = function (num) { num = num || 0; this.gainNode.gain.value = (this.volume += num) / 100; } //播放转为暂停 this.pauseAudio = function () { this.gainNode.disconnect(this.context.destination) } //暂停转为播放 this.playAudio = function () { this.gainNode.connect(this.context.destination) } //停止播放 this.stopAudio = function () { this.sourceAudio.stop() } //减小声音 this.decVolume = function () { if(this.volume >= 10){ this.changeVolume(-10); } } //增大声音 this.ascVolume = function () { if(this.volume <= 90){ this.changeVolume(10); } } //静音 this.quietVolume = function () { this.gainNode.gain.value = 0; } //静音恢复 this.recoverVolume = function () { this.changeVolume() } //当前音量 this.getVolume = function () { return (this.gainNode.gain.value).toFixed(2) * 100; } this.init(); return this; } window.test = new webAudio({ volume:100, url:'1.mp3', autoPlay:true, loopPlay:true }); //控制台事件 var pauseEle = document.getElementById("pause"); pauseEle.onclick = function() { if (pauseEle.alt === 'Pause') { test.pauseAudio(); } else { test.playAudio(); } }增加播放暂停图片切换
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值