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. 【解决】hive与hbase表结合级联查询的问题

    [Author]: kwu [解决]hive与hbase表结合级联查询的问题.hive两个表以上,关联查询时出现长时无法返回的情况. 同一时候也不出现,mr的进度百分比. 查询日志如图所看到的: 解决 ...

  2. 精确计算java中float和double的精度

    [本文相关的代码放在github上.地址为:https://github.com/VigourJiang/StructuredFloat] Java中double类型的格式基本遵循IEEE 754标准 ...

  3. STM32跑马灯

    #include "stm32f10x.h" #include "led.h" #include "delay.h" #include &q ...

  4. wordpress 配置(ubuntu)---修改 linux hostname

    使用阿里云服务器的 ubuntu 系统时的 hostname 太扭曲,而且有些命令会受 hostname 的影响不能正常使用,所以,一定要改掉它! 永久修改 hostname: 使用 nano 命令: ...

  5. sql 语句NVL()用法

    一NVL函数是一个空值转换函数 NVL(表达式1,表达式2) 如果表达式1为空值,NVL返回值为表达式2的值,否则返回表达式1的值. 该函数的目的是把一个空值(null)转换成一个实际的值.其表达式的 ...

  6. MSP430:AD10

    使用的MSP430G2553,为AD10,正常有8路输出,P1.0-P1.7为A0-A7 有七个寄存器, 参考电压可以是VCC或者内部参考电压1.5V或者2.5V 参考时钟可以是内部ADC10OSC ...

  7. MSXML2.XMLHTTP.4.0对象

    一.使用步骤:1.创建XMLHTTP对象 //需MSXML4.0支持2.打开与服务端的连接,同时定义指令发送方式,服务网页(URL)和请求权限等.客户端通过Open命令打开与服务端的服务网页的连接.与 ...

  8. Akka源码分析-ActorSystem

    由于本人对Akka比较感兴趣,也用Akka开发了一些系统,但对Akka的源码还没有具体分析过,希望研究源码的同时写一点博客跟大家分享.有不当之处还请指正.我准备采取Debug的方式来研究Akka的运行 ...

  9. mac 安装 swoole 可能会出现的错误

    请先看完之后再操作 一.用pecl安装swoole(没有安装起来) 2018年4月,由于homebrew的变动,导致无法使用brew install的方式安装php的扩展,现在改为用pecl安装,pe ...

  10. go 简单路由实现

    一.golang 路由实现的简单思路 1.http启动后,请求路径时走统一的入口函数 1.通过统一函数入口,获取request 的url路径 2.通过对url的路径分析,确定具体执行什么函数 二.统一 ...