Math.floor() 与 parseInt()
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()的更多相关文章
- Javascript -- Math.round()、Math.ceil()、Math.floor()、parseInt去小数取整总结
一.Math.round() 作用:四舍五入返回整数.(返回参数+0.5后,向下取整) Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round( ...
- 基础-Math.floor与parseInt区别
Math.floor只能对一个数向下取整,不能解析字符串 如: Math.floor(1.5) // 1 Math.floor(-2.1) // -3 Math.floor("3" ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结
Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...
- Number(),parseInt(),parseFloat(),Math.round(),Math.floor(),Math.ceil()对比横评
首先,这些处理方法可分为三类. 1,只用来处理数字取整问题的:Math.round(),Math.floor(),Math.ceil(): 2,专门用于把字符串转化成数值:parseInt(),par ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结
以前经常在代码中看到Math.round.parseInt.Math.floor和Math.ceil这四个函数,虽然知道结果都可以返回一个整数,但是对他们四者的区别还是不太清楚,今天就做一个小结. 一 ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结【转】
[摘要:之前常常正在代码中看到Math.round.parseInt.Math.floor战Math.ceil那四个函数,固然晓得效果皆能够返回一个整数,然则对他们四者的差别照样没有太清晰,本日便做一 ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)
js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数 ...
- codewars--js--Human Readable Time—Math对象,parseInt()
问题描述: Write a function, which takes a non-negative integer (seconds) as input and returns the time i ...
- 精灵方向移动问题[math.floor]
local xd = math.cos(math.rad(self._direction));--self._direction方向角度 local yd = math.sin(math.rad(se ...
随机推荐
- gem5: 使用ruby memory system中的mesh结构 出现AssertionError错误
问题:在使用ruby memory system中的mesh结构測试时,出现例如以下错误: Traceback (most recent call last): File "<stri ...
- HDU 3008 DP
基础DP题 打BOSS BOSS和自己都有100点血.玩家先手 每回合能够选择施放技能攻击(耗蓝,共n种)或者普通攻击(不耗蓝,伤害为1),BOSS每回合会攻击自己q点血,每回合自己会恢复t点法力 ...
- Android开发之利用SQLite进行数据存储
Android开发之利用SQLite进行数据存储 Android开发之利用SQLite进行数据存储 SQLite数据库简单介绍 Android中怎样使用SQLite 1 创建SQLiteOpenHel ...
- cocos2d的armature绑定到其它armature骨骼上的bug
在cocos2dx中,rmature的骨骼上能够绑定另外的armature,在我的项目中使用了该功能来完毕骑乘功能,可是在使用过程发现了例如以下的bug,特写在这里做一下记录. </span&g ...
- 微软将支持.net开源并跨平台,新特性会体现于VS2015
http://news.microsoft.com/2014/11/12/microsoft-takes-net-open-source-and-cross-platform-adds-new-dev ...
- Hybrid 开发
主讲人:吴彬 要学习某个东西之前,我们首先要了解这个东西是什么?然后我们要了解这东西有什么用,有什么好处和弊端?最后我们要知道这东西怎么用? 简单点就是 ——是什么?有什么用?怎么用? 那么进入正题 ...
- 【POJ 3974】 Palindrome
[题目链接] http://poj.org/problem?id=3974 [算法] 解法1 : 字符串哈希 我们可以分别考虑奇回文子串和偶回文子串,从前往后扫描字符串,然后二分答案,检验可以用哈希 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 70.资金管理-福利表管理 Extjs 页面
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...
- 关于pycharm中pip版本10.0无法使用的解决办法
背景: 近期在利用 pycharm 安装第三方库时会提示 pip 不是最新版本, 因此对 pip 进行更新,但是生成最新版本之后, pip 中由于缺少 main 函数,导致在 pycharm 中无法自 ...