当前位置:网站首页>JS - numerical processing (rounding, rounding, random numbers, etc.)
JS - numerical processing (rounding, rounding, random numbers, etc.)
2022-07-24 06:39:00 【Jie_ one thousand nine hundred and ninety-seven】
Numerical processing ( integer 、 rounding 、 Random number, etc )
One . Keep the data
1.1 Keep only the integer parts
parseInt(1.23456) // 1
1.2 Rounding down
Math.floor(1.23456) // 1
1.3 Rounding up
Math.ceil(1.23456) // 2
1.4 rounding
Math.round(1.23456) // 1
Math.round(1.73456) // 2
1.5 Take the absolute value
Math.abs(-1) // 1
1.6 Take the larger of the two numbers
Math.max(3,2) // 3
1.7 Take the smaller of two numbers
Math.min(3,2) // 2
Two . random number
2.1 random number (0 - 1)
contain 0, It doesn't contain 1
Math.random();
2.2 Random integers
Math.random()AndMath.floor()Use it together , Returns a random integer
- 0 to 10 ( contain 0, It doesn't contain 10)
Math.floor(Math.random() * 10) - 1 to 10 ( contain 1、10)
Math.floor(Math.random() * 10) + 1
2.3 A random integer between two numbers
- contain min , It doesn't contain max
function getRandomNum(min, max) { return Math.floor(Math.random() * (max - min) ) + min; } getRandomNum(1,2) // contain 1 , It doesn't contain 2 - contain min and max
function getRandomNum(min, max) { return Math.floor(Math.random() * (max - min + 1) ) + min; } getRandomNum(1,2) // contain 1、2
边栏推荐
猜你喜欢
随机推荐
FTP服务与实验
Metersphere one stop open source continuous testing platform
General paging 2.0
Flink function (2): checkpointedfunction
Flink time stream processing
Ia class summary (2)
openssl版本升级
Flink state use
General paging 01
磁盘管理和文件系统
Use of MySQL
Install Apache manually
NFS共享服务及实验
[226] instructions for Wireshark parameters
awk的使用
Leetcode sword finger offer jz42 maximum sum of continuous subarrays
OpenSSL version upgrade
ES10 subtotal flat and flatmap
Difference between PX and EM and REM
这10种定时任务学会了,直接飘









