温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JavaScript中使用取余运算符判断整数的方法

发布时间:2021-03-06 15:44:33 来源:亿速云 阅读:866 作者:小新 栏目:web开发

小编给大家分享一下JavaScript中使用取余运算符判断整数的方法,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

一、使用取余运算符判断

任何整数都会被1整除,即余数是0。利用这个规则来判断是否是整数。

function isInteger(obj) {
 return obj%1 === 0
}
isInteger(3) // true
isInteger(3.3) // false 

以上输出可以看出这个函数挺好用,但对于字符串和某些特殊值显得力不从心

isInteger('') // true
isInteger('3') // true
isInteger(true) // true
isInteger([]) // true

对于空字符串、字符串类型数字、布尔true、空数组都返回了true,真是难以接受。对这些类型的内部转换细节感兴趣的请参考:

JavaScript中奇葩的假值

因此,需要先 判断下对象是否是数字 ,比如加一个typeof

function isInteger(obj) {
 return typeof obj === 'number' && obj%1 === 0
}
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

嗯,这样比较完美了。

二、使用Math.round、Math.ceil、Math.floor判断

整数取整后还是等于自己。利用这个特性来判断是否是整数,Math.floor示例,如下

function isInteger(obj) {
 return Math.floor(obj) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

这个直接把字符串,true,[]屏蔽了,代码量比上一个函数还少。

三、通过parseInt判断

function isInteger(obj) {
 return parseInt(obj, 10) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

很不错,但也有一个缺点

isInteger(1000000000000000000000) // false

竟然返回了false,没天理啊。原因是parseInt在解析整数之前强迫将第一个参数解析成字符串。这种方法将数字转换成整型不是一个好的选择。

四、通过位运算判断

function isInteger(obj) {
 return (obj | 0) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

这个函数很不错,效率还很高。但有个缺陷,上文提到过,位运算只能处理32位以内的数字,对于超过32位的无能为力,如

isInteger(Math.pow(2, 32)) // 32位以上的数字返回false了

当然,多数时候我们不会用到那么大的数字。

五、ES6提供了Number.isInteger

Number.isInteger(3) // true
Number.isInteger(3.1) // false
Number.isInteger('') // false
Number.isInteger('3') // false
Number.isInteger(true) // false
Number.isInteger([]) // false

目前,最新的 Firefox 和Chrome已经支持。

六、js 输入int类型数字后自动在后面加.00

var getFloatStr = function (num) {
 num += '';
 num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
 if (/^0+/) //清除字符串开头的0
  num = num.replace(/^0+/, '');
 if (!/\./.test(num)) //为整数字符串在末尾添加.00
  num += '.00';
 if (/^\./.test(num)) //字符以.开头时,在开头添加0
  num = '0' + num;
 num += '00'; //在字符串末尾补零
 num = num.match(/\d+\.\d{2}/)[0];
 return num;
 };

如若vue 全局使用 在 main.js

Vue.prototype.getFloatStr = function(num) {
 num += '';
 num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符

 if (/^0+/) //清除字符串开头的0
 num = num.replace(/^0+/, '');
 if (!/\./.test(num)) //为整数字符串在末尾添加.00
 num += '.00';
 if (/^\./.test(num)) //字符以.开头时,在开头添加0
 num = '0' + num;
 num += '00'; //在字符串末尾补零
 num = num.match(/\d+\.\d{2}/)[0];
 return num;
}

方法有很多种 例如:

js将小数转为保留两位小数(保留0.00和不保留)

function toDecimal2(x) {
 var f = Math.round(x * 100) / 100;
 var s = f.toString();
 var rs = s.indexOf('.');
 if (rs < 0) {
 rs = s.length;
 s += '.';
 }
 while (s.length <= rs + 2) {
 s += '0';
 }
 return s;
}
//保留2位小数,如:2,还会保留2 不会补0
function toDecimal2NoZero(x) {
 var f = Math.round(x * 100) / 100;
 var s = f.toString();
 return s;
}

看完了这篇文章,相信你对“JavaScript中使用取余运算符判断整数的方法”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI