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 ...
随机推荐
- 1.7-BGP②
BGP的更新源(BGP Neighbor Update Source Address): 原则1: 在默认情况下, BGP路由器以自己路由表中,到达对方BGP邻居的地址的那条路由所指示的出接口(物理接 ...
- Win7 无法安装Office source engine 足够的权限安装系统服务怎么办
运行CMD,输入命令:sc delete ose 重试即可.
- 如何在 webpack 项目中使用绝对路径
react 小白编程 本项目是使用 create-react-app 脚手架构建的react项目 找到 config 配置文件下的 webpack.config.dev.js 和 webpack.co ...
- WPF 下 label 的刷新
WPF下,label控件并没有什么 Refresh() 的方法.那么现在问题就来了. 假设有这么个场景:WPF窗体上有一个按钮,一个Label,按下按钮,触发一些耗时的操作:在操作之前,Label显示 ...
- WIZnet的网络产品怎样选型
文章来源:成都浩然 我们在选用WIZnet的网络产品的时候.面对诸多的器件不知怎样选择,这里介绍一些方法以帮助project师高速准确地选择产品. WIZnet的产品有一个共同的特性.那就硬件TCPI ...
- bootstrap table load数据
直接load数据: $("#button").click(function(){ var name=$("input[name='name']").val(); ...
- oc73--NSArray使用
// // main.m // NSArray和NSString之间转换 #import <Foundation/Foundation.h> int main(int argc, cons ...
- had been doing 和had been done有什么差别
had been doing 和had been done有什么差别 浏览 37114 次 1个回答 最佳答案 21Doreen 来自科学教育类芝麻团 推荐于2017-10-15 1.首先要区分h ...
- XPath Nodes
教程 https://www.w3schools.com/xml/xpath_nodes.asp 节点之间的关系 Parent,Children,Siblings,Ancestors,Descenda ...
- AngularJS2.0 hello world例子——引入这么多额外的依赖库真是很忧伤啊
初识Angular2 写一个Angular2的Hello World应用相当简单,分三步走: 1. 引入Angular2预定义类型 import {Component,View,bootstrap} ...