数码资讯
用Javascript将浮点数转换成整数的三种方法
总结 我认为3种方法为时间。如果读者认为其他的方式来使用它,你也可以换。 parseInt 位运算符 math.floor math.ceil 描述 1。parseInt 1。实例 ParseInt(13nash ); / / 13 ParseInt(南)。 ParseInt(0xa ) / / 1
选购提示
关注价格、性能、续航、售后和真实使用场景,理性比较后再下单。
总结
我认为3种方法为时间。如果读者认为其他的方式来使用它,你也可以换。
parseInt
位运算符
math.floor math.ceil
描述
1。parseInt
1。实例
ParseInt(13nash ); / / 13
ParseInt(南)。
ParseInt(0xa ) / / 10(十六)
ParseInt(13) / / 13
ParseInt(070) / /沙56(八进制)ES5 70
两parseInt(070) / /沙和ES5 56
parseInt(22.5) / / 22
2。转换规则:
(1)。根据例(1),得出parseInt将解析到数不麻木。
(2)。根据一个实例(2),它的结论是,parseInt解析一个空字符串是南,不是0。
(3)。根据例(3),它的结论是,parseInt可以将16进制数10
(4)。根据例(4),parseInt忽略字符串的空间。
三.缺点:
(1)。根据一个实例(5)我们可以知道parseInt不兼容当转换八进制array.es3以070为一个八进制值,但ES5将看到070为十进制。
(2)。根据一个实例(6)(7)我们可以知道当parseInt执行时,参数将被转换成字符串,然后执行整数
4。解释为什么(5)(6)执行执行070转换成整数,但结果不一样这是如何解决的第二个缺点是。
Because in the official document I saw If string is not a string, then it is converted to one.This paragraph.That is to say, if the parameter is not a string, it will first convert it into a string and then convert it into an integer.For example, example (6) parseInt (070) is actually converting 070 to string first, you can try 070+ or String (070), you can know that 070 will be converted to 56Then it turns into parseInt (56), and the last number of integers is 56.Whether you are at ES3 or ES5 for 56
二位运算符
1。实例
console.log(0 | 123.45) / / 123
console.log(0 | 123.45) / / 123
console.log(0 ^ 123.45) / / 123
console.log(~ ~ 123.45) / / 123
2。原理:Javascript没有整数概念,所有数值类型都是双精度浮点数,当使用位运算符时,它首先将操作数转换为整数,便于操作,0或其他值是或不以或逐位改变操作值。
三、Math.floor和Math.ceil
1。实例
console.log(math.floor(2.3) / / 2
console.log(Math.floor(2.3) / / 3
console.log(math.ceil(2.3) / / 3
console.log(Math.ceil(2.3) / / 2
2。是不够的:Math.floor得到的数的最小整数;而Math.ceil得到的最大整数。所以我们有2.3圆我们想要得到的东西是2,但Math.floor得到了-3.and 2.3 math.ceil是3,但我们要的是2。
三.解决方案:
定义一个函数
函数库中的函数(Val){
返回值> 0 math.floor(Val):Math.ceil(Val);
}
我认为3种方法为时间。如果读者认为其他的方式来使用它,你也可以换。
parseInt
位运算符
math.floor math.ceil
描述
1。parseInt
1。实例
ParseInt(13nash ); / / 13
ParseInt(南)。
ParseInt(0xa ) / / 10(十六)
ParseInt(13) / / 13
ParseInt(070) / /沙56(八进制)ES5 70
两parseInt(070) / /沙和ES5 56
parseInt(22.5) / / 22
2。转换规则:
(1)。根据例(1),得出parseInt将解析到数不麻木。
(2)。根据一个实例(2),它的结论是,parseInt解析一个空字符串是南,不是0。
(3)。根据例(3),它的结论是,parseInt可以将16进制数10
(4)。根据例(4),parseInt忽略字符串的空间。
三.缺点:
(1)。根据一个实例(5)我们可以知道parseInt不兼容当转换八进制array.es3以070为一个八进制值,但ES5将看到070为十进制。
(2)。根据一个实例(6)(7)我们可以知道当parseInt执行时,参数将被转换成字符串,然后执行整数
4。解释为什么(5)(6)执行执行070转换成整数,但结果不一样这是如何解决的第二个缺点是。
Because in the official document I saw If string is not a string, then it is converted to one.This paragraph.That is to say, if the parameter is not a string, it will first convert it into a string and then convert it into an integer.For example, example (6) parseInt (070) is actually converting 070 to string first, you can try 070+ or String (070), you can know that 070 will be converted to 56Then it turns into parseInt (56), and the last number of integers is 56.Whether you are at ES3 or ES5 for 56
二位运算符
1。实例
console.log(0 | 123.45) / / 123
console.log(0 | 123.45) / / 123
console.log(0 ^ 123.45) / / 123
console.log(~ ~ 123.45) / / 123
2。原理:Javascript没有整数概念,所有数值类型都是双精度浮点数,当使用位运算符时,它首先将操作数转换为整数,便于操作,0或其他值是或不以或逐位改变操作值。
三、Math.floor和Math.ceil
1。实例
console.log(math.floor(2.3) / / 2
console.log(Math.floor(2.3) / / 3
console.log(math.ceil(2.3) / / 3
console.log(Math.ceil(2.3) / / 2
2。是不够的:Math.floor得到的数的最小整数;而Math.ceil得到的最大整数。所以我们有2.3圆我们想要得到的东西是2,但Math.floor得到了-3.and 2.3 math.ceil是3,但我们要的是2。
三.解决方案:
定义一个函数
函数库中的函数(Val){
返回值> 0 math.floor(Val):Math.ceil(Val);
}
声明:本文内容用于数码产品信息整理与选购参考,具体价格、库存、售后政策以官方渠道和电商页面实时信息为准。