04.数字的方法

130 阅读1分钟

数字的方法:

  1. console.log(Math);

数字方法的常用方法:

1.random

  1. 语法:Math.random()
  2. 作用:得到一个0-1之间的随机数,包含0
var res1 = Math.random();
console.log(res1);

2.round

  1. 语法:Math.round(数据)
  2. 作用:将数据四舍五入取整
// 四舍五入取整
// 结果123
var res2 = Math.round(123.123123);
console.log(res2);

3.ceil

  1. 语法:Math.ceil(数据)
  2. 作用:将数据向上取整
// 向上取整
// 结果124
var res3 = Math.ceil(123.123);
console.log(res3);

4.floor

  1. 语法:Math.floor(数据)
  2. 作用:将数据向下取整
// 向下取整
// 结果123
var res4 = Math.floor(123.123);
console.log(res4);

5.abs

  1. 语法:Math.abs(数据)
  2. 作用:取数据的绝对值
// 绝对值
// 结果123
var res5 = Math.abs(123);
console.log(res5);
// 结果123
var res6 = Math.abs(-123);
console.log(res6);

1.sqrt

  1. 语法:Math.sqrt(数据)
  2. 作用:求数据的平方根
// sqrt平方根
// 结果为2
var res1 = Math.sqrt(4);
console.log(res1);

2.pow

  1. 语法:Math.pow(底数 , 指数)
  2. 作用:求一个基数的x次幂
// pow三次幂
// 结果为8
var res2 = Math.pow(2 , 3);
console.log(res2);

3.max

  1. 语法:Math.max(参数1 , 参数2 , 参数3 , ...)
  2. 作用:求参数中的最大值
// max最大值
// 结果为12345
var res3 = Math.max(100 , 123 , 12345);
console.log(res3);

4.min

  1. 语法:Math.min(参数1 , 参数2 , 参数3 , ...)
  2. 作用:求参数中的最小值
// min最小值
// 结果为100
var res4 = Math.min( 100 , 123 , 12345 );
console.log(res4);

5.PI

  1. 语法:Math.PI
  2. 作用:求圆周率
// PI圆周率
// 结果为3.1415926...
var res5 = Math.PI;
console.log(res5);

注:在JavaScript中小数位一般保留17位 包括整数位和小数点。