JavaScipt中的Math.ceil() 、Math.floor() 、Math.round()、Math.pow() 三个函数的理解
以前一直会三个函数的使用产生混淆,现在通过对三个函数的原型定义的理解,其实很容易记住三个函数。
现在做一个总结:
1. Math.ceil()用作向上取整。
2. Math.floor()用作向下取整。
3. Math.round() 我们数学中常用到的四舍五入取整。
alert(Math.floor(5/2));
alert(Math.ceil(5/2));
4.幂
alert(Math.pow(10 , 2));//10的2次方
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>floatDecimal.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
//保留两位小数
//功能:将浮点数四舍五入,取小数点后2位
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return;
}
f = Math.round(x*100)/100;
return f;
}
//制保留2位小数,如:2,会在2后面补上00.即2.00 function toDecimal2(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return false;
}
var f = Math.round(x*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
} function fomatFloat(src,pos){
return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
} //四舍五入
alert("保留2位小数:" + toDecimal(3.14159267));
alert("强制保留2位小数:" + toDecimal2(3.14159267));
alert("保留2位小数:" + toDecimal(3.14559267));
alert("强制保留2位小数:" + toDecimal2(3.15159267));
alert("保留2位小数:" + fomatFloat(3.14559267, 2));
alert("保留1位小数:" + fomatFloat(3.15159267, 1));
//五舍六入
alert("保留2位小数:" + 1000.003.toFixed(2));
alert("保留1位小数:" + 1000.08.toFixed(1));
alert("保留1位小数:" + 1000.04.toFixed(1));
alert("保留1位小数:" + 1000.05.toFixed(1));
//科学计数
alert(3.1415.toExponential(2));
alert(3.1455.toExponential(2));
alert(3.1445.toExponential(2));
alert(3.1465.toExponential(2));
alert(3.1665.toExponential(1));
//精确到n位,不含n位
alert("精确到小数点第2位" + 3.1415.toPrecision(2));
alert("精确到小数点第3位" + 3.1465.toPrecision(3));
alert("精确到小数点第2位" + 3.1415.toPrecision(2));
alert("精确到小数点第2位" + 3.1455.toPrecision(2));
alert("精确到小数点第5位" + 3.141592679287.toPrecision(5));
</script>
</head>
<body>
This is my HTML page. <br>
</body>
</html>
JavaScipt中的Math.ceil() 、Math.floor() 、Math.round()、Math.pow() 三个函数的理解的更多相关文章
- Javascript Math ceil()、floor()、round()三个函数的区别
Round是四舍五入的...Ceiling是向上取整..float是向下取整. ceil():将小数部分一律向整数部分进位. 如: Math.ceil(12.2)//返回13 Math.ceil(12 ...
- 数学对象Math ceil()、floor()、round()方法
Math.ceil() 功能:对一个数进行上取整. 语法:Math.ceil(x) 参数: x:一个数值. 返回值:返回大于或等于x,并且与之最接近的整数. 注:如果x是正数,则把小数“入”: ...
- Jquery Math ceil()、floor()、round()比较与用法
Math.ceil():向上取值 如:Math.ceil(2.1) -- 结果为 3 Math.ceil(-2.1) -- 结果为-2 结论:正入 负舍 Math.floor(): 先下取值 入 ...
- Math.ceil()、floor()、round()
ceil():向上取整,>=某个小数的最小整数,即15.3取16.返回double类型 如果参数小于0且大于-1.0,结果为 -0. floor():向下取整,<=某个小数的最大整数,即1 ...
- python中的数字取整(ceil,floor,round)概念和用法
python中的数学运算函数(ceil,floor,round)的主要任务是截掉小数以后的位数.总体来说 就是取整用的.只是三者之间有微妙的区别: floor() :把数字变小 ceil() : ...
- php取整函数ceil,floor,round,intval函数的区别
开发过程中,遇到数据处理取整的时候,你会用哪个呢,小涛来介绍一下:PHP取整函数有ceil,floor,round,intval,下面详细介绍一下: 1.ceil — 进一法取整说明float cei ...
- [转]PHP取整函数:ceil,floor,round,intval的区别详细解析
我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. 1.ceil -- 进一法取整 说明float ceil ( float value ) 返回不小于 value ...
- PHP 浮点数 转化 整数方法对比 ceil,floor,round,intval,number_format
ceil,floor,round,intval,number_format - 执行1000W此效率对比 Header("Content-Type:text/html;charset=utf ...
- JS单体内置对象之Math常用方法(min,max,ceil,floor,round,random等)
1.min()和max()方法 Math.min()用于确定一组数值中的最小值.Math.max()用于确定一组数值中的最大值. alert(Math.min(2,4,3,6,3,8,0,1,3)); ...
随机推荐
- pip解决超时问题(timeout)
我们下载python的库一般会使用pip工具.但在下载的过程中经常会timeout,这是因为资源在国外,我们国内某些资源下载速度特别慢,主要有两种方法解决. 一.设置pip timeout超时时间 创 ...
- Spring @RequestAttribute
@RequestAttribute注解用法 @RequestAttribute用在方法入参上,作用:从request中取对应的值,至于request中是怎么存在该属性的,方式多种多样,拦截器中预存.M ...
- flask数据库迁移理解及命令
前言: 使用数据库迁移,可以直接建表,而不用我们自己写sql语句用来建表.就是将关系型数据库的一张张表转化成了Python的一个个类. 在开发中经常会遇到需要修改原来的数据库模型,修改之后更新数据库, ...
- VS中C#的快捷键
Ctrl+E,D: 格式化全部代码 Ctrl+E,C / Ctrl+K,C: 注释选定内容 Ctrl+E,U / Ctrl+K,U: 取消选定注释内容 Ctrl+E,S: 查看空白 Ctrl+E,W: ...
- JavaScript字符串常用方法
toUpperCase():把一个字符串全部变为大写 toLowerCase():把一个字符串全部变为小写 indexOf():会搜索制定字符串出现的位置,有返回索引,没有返回-1 substring ...
- 使用Git Extensions简单入门Git
前言 关于这个主题,之前我录了段视频教程,在本地看清晰度还可以,但传到优酷上就很不清晰了,即使是后来重制后还是一样不清晰,所以现在想整理成文字版.当然,大家还可以将我百度云上的视频下载下来观看,连同优 ...
- 时间戳转日期 mysql以及sql server 用法
一.mysql UNIX时间戳转换为日期函数:FROM_UNIXTIME() eg:select FROM_UNIXTIME(1156219870) 结果会输出 2006-08-22 12:11:10 ...
- Angular Forms - 自定义 ngModel 绑定值的方式
在 Angular 应用中,我们有两种方式来实现表单绑定--"模板驱动表单"与"响应式表单".这两种方式通常能够很好的处理大部分的情况,但是对于一些特殊的表单控 ...
- That Nice Euler Circuit(LA3263+几何)
That Nice Euler Circuit Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu D ...
- Spring全家桶系列–SpringBoot之入门JPA
//本文作者:cuifuan 什么是JPA? 一种规范,并非ORM框架,也就是ORM上统一的规范 用了之后可以做什么,为什么要用? 代码解释: 实体类 package com.example.spri ...