Math(js) ╰+攻爆jí腚メ 2023-10-03 14:23 27阅读 0赞 Math用法 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Math</title> <script> // Math数学对象 不是一个构造函数 ,所以我们不需要new 来调用 而是直接使用里面的属性和方法即可 console.log(Math.PI); // 一个属性 圆周率 console.log(Math.max(1, 99, 3)); // 99 console.log(Math.max(-1, -10)); // -1 console.log(Math.max(1, 99, '老师')); // NaN console.log(Math.max()); // -Infinity </script> </head> <body> </body> </html> Math绝对和三个取整方法 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Math绝对和三个取整方法</title> <script> // 1.绝对值方法 console.log(Math.abs(1)); // 1 console.log(Math.abs(-1)); // 1 console.log(Math.abs('-1')); // 隐式转换 会把字符串型 -1 转换为数字型 console.log(Math.abs('k')); // NaN // 2.三个取整方法 // (1) Math.floor() 地板 向下取整 往最小了取值 console.log(Math.floor(1.1)); // 1 console.log(Math.floor(1.9)); // 1 // (2) Math.ceil() ceil 天花板 向上取整 往最大了取值 console.log(Math.ceil(1.1)); // 2 console.log(Math.ceil(1.9)); // 2 // (3) Math.round() 四舍五入 其他数字都是四舍五入,但是 .5 特殊 它往大了取 console.log(Math.round(1.1)); // 1 console.log(Math.round(1.5)); // 2 console.log(Math.round(1.9)); // 2 console.log(Math.round(-1.1)); // -1 console.log(Math.round(-1.5)); // 这个结果是 -1 </script> </head> <body> </body> </html> Matg对象随机数方法 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Matg对象随机数方法</title> <script> // 1.Math对象随机数方法 random() 返回一个随机的小数 0 =< x < 1 // 2. 这个方法里面不跟参数 // 3. 代码验证 console.log(Math.random()); // 4. 我们想要得到两个数之间的随机整数 并且 包含这2个整数 // Math.floor(Math.random() * (max - min + 1)) + min; function getRandom(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } console.log(getRandom(1, 10)); // 5. 随机点名 var arr = ['张三', '张三丰', '张三疯子', '李四', '李思思', 'k老师']; // console.log(arr[0]); console.log(arr[getRandom(0, arr.length - 1)]); </script> </head> <body> </body> </html>
相关 JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js 一、计算精度现象举例 举例1、加法 ![091c490fbcae4c88b917b442c8df45af.png][] 举例2、减法 ![7a31996d3e3a474 Myth丶恋晨/ 2024年03月26日 00:27/ 0 赞/ 164 阅读
相关 数学公式库mathjs 安装使用教程 Math.js 是个JavaScript 和 Node.js 的扩展数学库。它包括了灵活的表达式解析器,提供数字,大数值,复杂数值,单位,矩阵等等集成的解决方案。Math.js 骑猪看日落/ 2024年03月16日 21:07/ 0 赞/ 132 阅读
相关 mathjs--使用/实例 原文网址:[mathjs--使用/实例\_IT利刃出鞘的博客-CSDN博客][mathjs--_IT_-CSDN] 简介 说明 本文介绍JavaScript的数学计算 系统管理员/ 2023年09月28日 09:09/ 0 赞/ 38 阅读
相关 mathjs - 解决小数点计算准确问题 <script src="https://cdn.bootcdn.net/ajax/libs/mathjs/7.2.0/math.min.js"></script> 本是古典 何须时尚/ 2022年11月07日 05:57/ 0 赞/ 291 阅读
还没有评论,来说两句吧...