当前位置:网站首页>JS - 数值处理(取整、四舍五入、随机数等)
JS - 数值处理(取整、四舍五入、随机数等)
2022-07-24 05:17:00 【Jie_1997】
数值处理(取整、四舍五入、随机数等)
一. 保留数据
1.1 只保留整数部分
parseInt(1.23456) // 1
1.2 向下取整
Math.floor(1.23456) // 1
1.3 向上取整
Math.ceil(1.23456) // 2
1.4 四舍五入
Math.round(1.23456) // 1
Math.round(1.73456) // 2
1.5 取绝对值
Math.abs(-1) // 1
1.6 取两数中的较大值
Math.max(3,2) // 3
1.7 取两数中的较小值
Math.min(3,2) // 2
二. 随机数
2.1 随机数(0 - 1)
包含0,不包含1
Math.random();
2.2 随机整数
Math.random()与Math.floor()一起使用,返回随机整数
- 0 至 10 (包含0,不包含10)
Math.floor(Math.random() * 10) - 1 至 10 (包含1、10)
Math.floor(Math.random() * 10) + 1
2.3 两个数之间的随机整数
- 包含 min ,不包含 max
function getRandomNum(min, max) { return Math.floor(Math.random() * (max - min) ) + min; } getRandomNum(1,2) //包含 1 ,不包含 2 - 包含 min 和 max
function getRandomNum(min, max) { return Math.floor(Math.random() * (max - min + 1) ) + min; } getRandomNum(1,2) //包含 1、2
边栏推荐
猜你喜欢
随机推荐
数据类型概括
scikit-learn笔记
4. 在屏幕上绘制一个红色三角形,一个黄色正方形。三角形在后,小;正方形在前,大。使用融合技术,使得可以透过正方形看到三角形,源和目标融合因子分别为GL_SRC_ALPHA和GL_ONE_MINUS
Collation of commonly used Anaconda commands
Opengl模拟现实生活中,球掉到地面上再弹起来的过程,在屏幕上绘制一个球,球从上往下掉,碰到地面,再弹起来。
JS - 计算直角三角形的边长及角度
C语言进阶篇 一.数据的存储
你真的知道判断语句吗?
Neo4j修改标签名
【Pytorch】conv2d torchvision.transforms
web开发
CentOS 7安装Mysql5.6.37
Tips for using BeanShell built-in variable prev
Tips for using the built-in variable vars in BeanShell
深度剖析数据在内存中的存储
scikit-learn笔记
C#进程运行权限
谈谈对未来的想法
Redis的使用
【sklearn】RF 交叉验证 袋外数据 参数学习曲线 网格搜索









