前言

javascript中的Math对象作为保存数学公式、信息和计算的地方。
Math对象提供了一些辅助计算的属性和方法,在我们日常使用都是**Math.ceil()、Math.round()**等较多。

为啥没有new Math()这种操作呢?

因为啊Math()Date()等这种对象不一样,它在我们的javascript引擎中就已经帮我们new好了,所以我们在使用的过程中也就不再需要new Math(),这种操作了,我们直接Math.xx这样使用就可以了。

本篇文章将对Math对象的一些api进行系列的详解和使用。

let’s go,让我们步入正题

1、min() 方法

用来确定一组数值中的最小值

1
2
let min = Math.min(3, 54, 32, 16);
console.log(min); // 3

2、max() 方法

用来确定一组数值中的最大值

1
2
let max = Math.max(3, 54, 32, 16);
console.log(max); // 54

2、舍入

1. Math.ceil()

方法始终向上舍入为最接近的整数

1
2
console.log(Math.ceil(25.9));    // 26
console.log(Math.ceil(25.1)); // 26
2. Math.floor()

始终向下舍入为最接近的整数

1
2
console.log(Math.floor(25.9));   // 25
console.log(Math.floor(25.1)); // 25
3. Math.round()

方法执行四舍五入

1
2
console.log(Math.round(25.6));   // 26
console.log(Math.round(25.1)); // 25
4. Math.fround()

返回数值最接近的单精度(32位)浮点值表示

1
2
console.log(Math.fround(0.5));   // 0.5
console.log(Math.fround(25.9)); // 25.899999618530273

3、random()

返回一个0~1范围内的随机数,其中包含0但不包含1。

1
2
console.log(Math.random());   // 0.7200208040406486
console.log(Math.random()); // 0.3164350705812933

需要1 - 10之间的数就直接将返回值 × 10,如果是需要0 - 100 间的数则 × 100,以此类推

1
2
console.log(Math.random() * 10) //7.173275197566868
console.log(Math.random() * 10) //9.037917861672913

4、其他方法

Math还有的一些属性和方法,在我们的日常使用中可以说是没用,甚至没见过,这里我就直接通过图片列出来,以供了解

请添加图片描述

以上内容就是jsMath的方法api的使用及介绍。