parseInt()与Math.floor()都能实现数字的向下取整,但是两者存在根本上的差异,
1.Math.floor()
用于一个数的向下取整,不能解析字符串
<script type="text/javascript">
document.write(Math.floor(0.89) + "<br />")  //结果0
document.write(Math.floor(-0.2) + "<br />")  //结果-1
document.write(Math.floor(-4.3) + "<br />")  //结果-5
document.write(Math.floor("3") + "<br />")  //结果3
document.write(parseInt(2*4) + "<br />")  //结果8
document.write(Math.floor("hello") + "<br />")  //结果NaN
document.write(parseInt("760px");  //结果NaN
</script>
2.parseInt()
把任意字符串转换为整数(必须以数字开头)
<script type="text/javascript">
document.write(parseInt(0.89) + "<br />")  //结果0
document.write(parseInt(-0.2) + "<br />")  //结果0
document.write(parseInt(-4.3) + "<br />")  //结果-4   
document.write(parseInt("3") + "<br />")  //结果3 
document.write(parseInt(2*4) + "<br />")  //结果8
document.write(parseInt("hello") + "<br />")  //结果NaN 
document.write(parseInt("760px");  //结果760
</script>

3.parseInt(string, radix)

可以把二进制、八进制、十六进制或其他任何进制的字符串转换成整数,默认转化为十进制。

<script type="text/javascript"> 

document.write(parseInt("12",10) + "<br />")  //结果12
document.write(parseInt("12",8) + "<br />")  //结果10
document.write(parseInt("12",2) + "<br />")  //结果1  
document.write(parseInt("A",10) + "<br />")  //结果NaN
document.write(parseInt("A",16) + "<br />")  //结果10
</script>

归纳说明

1)、Math.floor对正数的小数取“舍”,对负数的小数取“入”;

2)、praseInt属于类型转换,会对字符逐级判断,占用内存较高;

3)、两者的用途、用法都不相同,尽量避免混合使用

Math.floor() 与 parseInt()的更多相关文章

  1. Javascript -- Math.round()、Math.ceil()、Math.floor()、parseInt去小数取整总结

    一.Math.round() 作用:四舍五入返回整数.(返回参数+0.5后,向下取整) Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round( ...

  2. 基础-Math.floor与parseInt区别

    Math.floor只能对一个数向下取整,不能解析字符串 如: Math.floor(1.5) // 1 Math.floor(-2.1) // -3 Math.floor("3" ...

  3. js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结

    Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...

  4. Number(),parseInt(),parseFloat(),Math.round(),Math.floor(),Math.ceil()对比横评

    首先,这些处理方法可分为三类. 1,只用来处理数字取整问题的:Math.round(),Math.floor(),Math.ceil(): 2,专门用于把字符串转化成数值:parseInt(),par ...

  5. js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结

    以前经常在代码中看到Math.round.parseInt.Math.floor和Math.ceil这四个函数,虽然知道结果都可以返回一个整数,但是对他们四者的区别还是不太清楚,今天就做一个小结. 一 ...

  6. js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结【转】

    [摘要:之前常常正在代码中看到Math.round.parseInt.Math.floor战Math.ceil那四个函数,固然晓得效果皆能够返回一个整数,然则对他们四者的差别照样没有太清晰,本日便做一 ...

  7. js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)

    js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数 ...

  8. codewars--js--Human Readable Time—Math对象,parseInt()

    问题描述: Write a function, which takes a non-negative integer (seconds) as input and returns the time i ...

  9. 精灵方向移动问题[math.floor]

    local xd = math.cos(math.rad(self._direction));--self._direction方向角度 local yd = math.sin(math.rad(se ...

随机推荐

  1. 走进windows编程的世界-----画图相关

    Windows画图 1 图形绘制      1.1 图形绘制的方式      获取到画图句柄-设备描写叙述表(DC),使用对应的画图的API,在设备上绘制图形.          1.2 颜色     ...

  2. 解决安装OpenShift Client Tools时提示的dl/import (LoadError)问题

    安装成功Ruby和git以后.执行rhc setup时提演示样例如以下错误: C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require ...

  3. unity3D游戏开发实战原创视频讲座系列11之相扑游戏开发并公布到Win\WP8

     解说文件夹 第一讲 游戏的演示和资源介绍 第二讲 场景的建设 第三讲 玩家的移动 第四讲 对手的AI(让对手动起来) 第五讲 游戏的管理(上) 第六讲 游戏的管理(下) 第七讲 公布到Win8系 ...

  4. How do browser cookie domains work?

    https://stackoverflow.com/questions/1062963/how-do-browser-cookie-domains-work 答案一 Although there is ...

  5. CNN 文本分类模型优化经验——关键点:加卷积层和FC可以提高精度,在FC前加BN可以加快收敛,有时候可以提高精度,FC后加dropout,conv_1d的input维度加大可以提高精度,但是到256会出现OOM。

    network = tflearn.input_data(shape=[None, max_len], name='input') network = tflearn.embedding(networ ...

  6. MAC地址 初识

    MAC地址 即物理地址/硬件地址 地址长度为48位,6字节. 格式为:00-23-5A-15-99-42 一个网卡对应一个MAC地址(比如笔记本,有线网卡有一个MAC地址,无线网卡也有一个MAC地址) ...

  7. [NOI1997] 积木游戏(dp)

    COGS 261. [NOI1997] 积木游戏 http://www.cogs.pro/cogs/problem/problem.php?pid=261 ★★   输入文件:buildinggame ...

  8. $P2126 Mzc家中的男家丁$

    problem #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; typedef long long LL ...

  9. CentOS 7.0 firewall防火墙关闭firewall作为防火墙,这里改为iptables防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤: 1.先检查是否安装了: iptables service iptables status 2.安装ip ...

  10. C# linq学习【转】

    在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...