Math对象常用方法(取整细节)

Math 对象

Math 对象用于执行数学任务。

1.常用属性:

  1.E :返回算术常量e,即自然对数的底数(约2.718)

  2.PI :返回圆周率,约3.14159

2.常用方法    Math.方法()  调用即可

  1.abs(x)  返回绝对值

  2.ceil(x)   上舍入

  3.floor(x)  下舍入

  4.round(x)  四舍五入为最近的整数

  5.random()  返回0~1之间的随机数

  6.max(x,y)  返回x,y中最高值

  7.min(x,y)   返回x,y中最低值

  8.pow(x,y)  返回x的y次幂

  9.sqrt(x)    返回x的平方根

  

 向下取整(舍掉小数)

  Math.floor(2)=2
  Math.floor(2.9)=2
  Math.floor(-2.1)=-3
  Math.floor(-2.9)=-3

 向上取整(凑整)

  Math.ceil(2)=2
  Math.ceil(2.1)=3
  Math.ceil(2.5)=3
  Math.ceil(2.9)=3

  Math.ceil(-2)=-2
  Math.ceil(-2.1)=-2
  Math.ceil(-2.5)=-2
  Math.ceil(-2.9)=-2

  四舍五入(取最近整数)

  Math.round(3.14)=3
  Math.round(3.5)=4
  Math.round(-3.14)=-3
  Math.round(-3.51)=-4

 注意:对于 0.5,该方法将进行上舍入

 例如,3.5 将舍入为 4,而 -3.5 将舍入为 -3。

  Math.random()返回指定min~max之间的随机数

  var x = Math.floor(Math.random()*(max - min + 1)) + min;

从数组中的随机获取成员

  var items = [some 成员];

  var randomItem = items[Math.floor(Math.random() * items.length)];

时间: 2024-08-10 00:06:19

Math对象常用方法(取整细节)的相关文章

JS取整,四舍五入,取绝对值等Math对象常用方法

原文: http://hail812.blog.163.com/blog/static/174581211201212292515749/ <script type="text/javascript"> function f1(type,num1) {     switch(type)     {         case 'floor':             return Math.floor(num1);//取整或下舍入             break;    

js Math对象常用方法

//js Math对象常用方法// 一组数字求最大最小值var max=Math.max(1,2,3,4,8,-9);//求最大值var min=Math.min(1,2,3,4,8,-9);//求最小值console.log(max,min);//返回 8 -9 注意:如果参数中出现字符串 则返回NaN //取整var num=18.1;var num2=18.9;//向上取整 Math.ceil()console.log(Math.ceil(num),Math.ceil(num2));//返

js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)

js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整. 如: Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round(-1.5) //返回-1 Math.round(-5.8) //返回-6 二.

js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结

Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整. 如: Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round(-1.5) //返回-1 Math.round(-5.8) //返回-6 二.parseInt 作用:解析一个字符串,并返回一个整数,这里可以简单理解成返回舍去参数的小数部分后的

JavaScript基础 Math.ceil() 向上取整 小数部分不四舍五入了,有小数就入。小数再小,都进位

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=ut

Math对象常用方法

1.Math.ceil(x) 返回x的向上取整. var a=Math.ceil(9.1); var b=Math.ceil(-9.1) console.log(a); //10 console.log(b); //-9 2.Math.floor(x) 返回x的向下取整. var a=Math.floor(9.1); var b=Math.floor(-9.1) console.log(a); //9 console.log(b); //-10 3.Math.round(x) 返回x四舍五入后的

Math.round、parseInt、Math.floor和Math.ceil小数取整小结

以前经常以代码中看到Math.round.parseInt和Math.floor这三个函数,虽然知道结果最后都是返回一个整数,但是对他们三者的区别还是不太清楚,今天就做一个小结. 一.Math.round 作用:四舍五入,返回最接近参数的整数,如果小数部分大于等于0.5,返回大于参数的最小整数,否则返回小于等于参数的最大整数. 如: Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round(-1.5) //返回-1 二.parseInt 作用

取整函数和取整运算符的细节比较

>>;~~;Math.floor();Math.ceil();parseInt();都可以做到取整的效果,下面看看他们具体的不同点: 1 console.log(-2.6>>0); 2 console.log(2.6>>0); 3 console.log(~~-2.6); 4 console.log(~~2.6); 5 6 /*number>0 ? Math.floor(number) : Math.ceil(number);*/ 7 8 console.log(

js最基础知识回顾6(数组,JSON,getByClass,select,Math对象)

一.数组的相关操作 1. 定义 (1)var arr=[1,'abc',function(){alert(3333);},[333,444]]; (2)var arr=new Array(12,5,8,9);   如果只放一个数字,要设定length (3)[]的性能略高,因为代码短 2. 属性----length(既可以获取,又可以设置)---例子:快速清空数组 var a=[1,2,3,4,5,6,]; a.length=0; alert(a); (1)如果设置的length多于数组的内容,