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. 苹果iOS手机后门的”诊断功能论“不攻自破

    7月23日.苹果公司公布公告,题为"iOS:About diagnostic capabilities"("iOS:关于诊断功能").当中声称:iOS offe ...

  2. 转 java面试题

    ● 简述synchronized?Object:Monitor机制: ● 简述happen-before规则 : ● JUC和Object : Monitor机制区别是什么 : 简述AQS原理 : ● ...

  3. MICRO SIM卡(SIM小卡)尺寸图及剪卡图解

    如今使用MICRO SIM卡的手机越来越多.近期刚刚买了一个手机到手才发现尼马使用的是MICRO SIM卡.再去买剪卡器吧,十几二十块用一次就废了,去营业厅吧.又比較远,懒的出门.怎么办呢,自己剪!这 ...

  4. react State改变,页面却没有改变

    react 小白编程 做项目时遇到了个问题,无论我怎么查看我的action.reducer 还是 dispatch 函数,都没有发现有什么毛病.但是 debugger 的时候,state 改变了,页面 ...

  5. 金典 SQL笔记 SQL语句汇总

    SELECT * FROM (SELECT ROW_NUMBER() OVER ( ORDER BY id) AS rowN,FNumber, FName,FSalary ,Fage FROM dbo ...

  6. Android之实现ViewPager+Fragment左右滑动

    近期看新闻发现新闻的页面是能够左右滑动的.于是自己就好奇起来了,之前做过ViewPager展示图片,在想怎么载入页面呢?研究了一下.发现就是加入了Fragment,废话不多说,揭秘奥秘的时候到了. 使 ...

  7. apache ua Custom Log Formats

    RewriteEngine OnRewriteBase / RewriteCond %{HTTP_USER_AGENT} (android|bb\d+|meego).+mobile|avantgo|b ...

  8. charts柱状图,定时刷新

    option: var dataoptionone = { title : { text: '数据存储情况', subtext: '数据表', x:'center' }, tooltip: { sho ...

  9. mac 配置hadoop 2.6(单机和伪分布式)

    一.准备工作: 安装jdk >= 1.7: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...

  10. bzoj3175: [Tjoi2013]攻击装置&&4808: 马

    终于知道为啥网络流这么受欢迎了. 其实就是构个图模板一下的事儿,比较好打是吧. 然后这题网络流黑白染色(其实感觉上匈牙利更加直接好想啊,但是实际上黑白染色给人感觉就是二分图) st连白而ed连黑,流量 ...