JS中Math函数的常用方法

news/2024/7/19 13:40:07 标签: js

Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’
console.dir(Math) 查看Math的所有函数方法。
1,Math.abs() 获取绝对值

js">Math.abs(-12) = 12

2,Math.ceil() and Math.floor() 向上取整和向下取整

3,Math.round() 四舍五入
注意:正数时,包含5是向上取整,负数时包含5是向下取整。

js">Math.round(-16.3) = -12
Math.round(-16.5) = -12
Math.round(-16.51) = -13

4,Math.random() 取[0,1)的随机小数
案例1:获取[0,10]的随机整数

js">Math.round(Math.random()*10)

案例2:获取[n,m]之间的随机整数

js">Math.round(Math.random()*(m-n)+n)

5,Math.max() and Max.min() 获取一组数据中的最大值和最小值

6,Math.PI 获取圆周率π 的值

7,Math.pow() and Math.sqrt()
Math.pow()获取一个值的多少次幂
Math.sqrt()对数值开方

在这里插入图片描述


http://www.niftyadmin.cn/n/1355486.html

相关文章

自定义日历样式,每日签到

需求:两种日历签到样式,周日历,以及月日历签到。是用框架vue-cli 月日历代码 export default {data () {return { // 所需的属性startTime: 2019-08-15,year: 0,month: 0,weekMonth: 0,day: 0,newDate: new Date().format(yyyy-MM-dd),wee…

如何快速判断是否在容器环境

在渗透测试过程中,我们的起始攻击点可能在一台虚拟机里或是一个Docker环境里,甚至可能是在K8s集群环境的一个pod里,我们应该如何快速判断当前是否在容器环境中运行呢? 当拿到shell权限,看到数字和字母随机生成的主机名…

node.js生成目录树

首先,大体先看一下要生成的目录树的样子 ├─ hw│ ├─dirtree.js│ └─ test.js├─test.js└─ url.js大体思路分析 1、获取目标文件夹的目录内容 2、将文件或者文件夹进行划分 3、将文件或者文件夹进行打印 命令行console.log() 4、中间要用到的关键就是读取…

使用nodejs爬取网页图片

用nodejs作简易的爬虫 爬取网页图片并下载到本地,代码如下: const https require(https) // const http require(http) /* 方式二时使用*/ const fs require(fs) const cheerio require(cheerio) const request require(request) const path req…

vue-cli3配置less以及相关问题解决

第一种配置方式 npm install style-resources-loader vue-cli-plugin-style-resources-loader less-loader less -S// vue.config.js 文件配置 const path require(path) module.exports {pluginOptions: {style-resources-loader: {preProcessor: less,patterns: [path.res…

js 获取select的值 / js动态给select赋值

jQuery获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 2. var checkText$("#select_id").find("option:selected").text(); //…

HTML5 jQuery图片上传前预览

<!DOCTYPE html><html><head><title>HTML5上传图片预览</title><meta http-equiv"Content-Type" content"text/html; charsetUTF-8"><script src"http://www.codefans.net/ajaxjs/jquery-1.6.2.min.js"&…

js字符串转换成数字,数字转换成字符串

将字符串转换成数字&#xff0c;得用到parseInt函数。parseInt(string) &#xff1a; 函数从string的开始解析&#xff0c;返回一个整数。 举例&#xff1a;parseInt(123) : 返回 123&#xff08;int&#xff09;&#xff1b; parseInt(1234xxx) : 返回 1234&#xff08;int&am…