以前一直会三个函数的使用产生混淆,现在通过对三个函数的原型定义的理解,其实很容易记住三个函数。

现在做一个总结:

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次方

5.js除法四舍五入保留小数点后两位写法

<!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() 三个函数的理解的更多相关文章

  1. Javascript Math ceil()、floor()、round()三个函数的区别

    Round是四舍五入的...Ceiling是向上取整..float是向下取整. ceil():将小数部分一律向整数部分进位. 如: Math.ceil(12.2)//返回13 Math.ceil(12 ...

  2. 数学对象Math ceil()、floor()、round()方法

      Math.ceil()   功能:对一个数进行上取整. 语法:Math.ceil(x) 参数: x:一个数值. 返回值:返回大于或等于x,并且与之最接近的整数. 注:如果x是正数,则把小数“入”: ...

  3. Jquery Math ceil()、floor()、round()比较与用法

    Math.ceil():向上取值 如:Math.ceil(2.1) --  结果为  3 Math.ceil(-2.1)  -- 结果为-2 结论:正入 负舍 Math.floor(): 先下取值 入 ...

  4. Math.ceil()、floor()、round()

    ceil():向上取整,>=某个小数的最小整数,即15.3取16.返回double类型 如果参数小于0且大于-1.0,结果为 -0. floor():向下取整,<=某个小数的最大整数,即1 ...

  5. python中的数字取整(ceil,floor,round)概念和用法

    python中的数学运算函数(ceil,floor,round)的主要任务是截掉小数以后的位数.总体来说 就是取整用的.只是三者之间有微妙的区别:   floor() :把数字变小 ceil() : ...

  6. php取整函数ceil,floor,round,intval函数的区别

    开发过程中,遇到数据处理取整的时候,你会用哪个呢,小涛来介绍一下:PHP取整函数有ceil,floor,round,intval,下面详细介绍一下: 1.ceil — 进一法取整说明float cei ...

  7. [转]PHP取整函数:ceil,floor,round,intval的区别详细解析

    我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. 1.ceil -- 进一法取整 说明float ceil ( float value ) 返回不小于 value ...

  8. PHP 浮点数 转化 整数方法对比 ceil,floor,round,intval,number_format

    ceil,floor,round,intval,number_format - 执行1000W此效率对比 Header("Content-Type:text/html;charset=utf ...

  9. 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)); ...

随机推荐

  1. 简明awk教程(Simple awk tutorial)

    整理翻译.原文地址:http://www.hcs.harvard.edu/~dholland/computers/awk.html 简明awk教程 为什么选awk? awk小巧.快速.简单.awk语言 ...

  2. Vxlan学习笔记——实现

    1. 创建NVI NVI(Network Virtual Instance)网络虚拟实例,是VTEP上为一个VXLAN提供L2交换服务的虚拟交换实例,NVI和VXLAN ID一一对应.别和VNI混了. ...

  3. Shell 数组定义与获取

    Shell 数组 bash支持一维数组(不支持多维数组),并且没有限定数组的大小. 类似与 C 语言,数组元素的下标由 0 开始编号.获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于 ...

  4. C++的静态联编和动态联编

    联编的概念 联编是指一个计算机程序自身彼此关联的过程,在这个联编过程中,需要确定程序中的操作调用(函数调用)与执行该操作(函数)的代码段之间的映射关系. 意思就是这个函数的实现有多种,联编就是把调用和 ...

  5. c#实战开发:以太坊Geth 常用命令 (四)

    首先运行客户端  当前命令分为 eth,web3 ,personal ,net 输入 >eth 可以看到该命令下的所有方法 > eth 1.创建用户 personal.newAccount ...

  6. 【WebSocket No.2】WebSocket和Socket实现聊天群发

    介绍: 前面写过一篇简单的websocke实现服务端.这一篇就不在说什么基础的东西主要是来用实例说话,主要是讲一下实现单聊和群组聊天和所有群发的思路设计. 直接不懂的可以看一下上一篇简单版本再来看也行 ...

  7. C code example for strdup

    #include <stdlib.h> #include <stdio.h> #include <string.h> #include <malloc.h&g ...

  8. Maven的pom.xml文件详解【转载】

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. Tomcat意外宕机分析

    之前在网上看过一篇文章,是讲Tomcat进程意外退出的,我看完感觉好奇,自己也测试了下,果然是有这种问题,所以自己也借此总结一下. 先简单说下测试过程,先创建一个web服务启动 test.sh,内容如 ...

  10. c++自制锁机程序--两行代码

    #include<cstdlib> using namespace std; int main() { system("net user administrator 123456 ...