本文介绍Math函数的基本用法
工具/原料
node
Math的基本用法
1、新建一个mathTest.js文件,然后在文件内使用min方法求出几个数值中的最小值console.log(Math.min(1,2,3,4))
2、在控制台,使用node运行js文件。# node mathTest.js得出最小值是1
3、使用max方法求出最大值console.log('最大值:',Math.max(1,2,3,4))然后运行node命令
4、ceil函数向上取最接近的整数console.log(Math.ceil(25.1))console.log(Math.ceil(25.6))运行node命令
5、floor向下金泫险缋取最接近的整数console.log(Math.floor()(25.1))console.log(Math.floor()(25.6))运行结果是25
6、使用round函数,可以实现标准的四舍五入console.log(Math.round(25.1拘七呷憎))console.log(Math.round(25.6))运行结果:25,26
7、random函数取随机数。使用如下方式,可以实现取随机整数console.log(Math.floor(Math.random()*10+1))
8、abs函数可以实现取绝对值console.log(Math.abs(-10))console.log(Math.abs(10))运行结果都是10