首页 > 其他分享 >js Math汇总

js Math汇总

时间:2023-04-07 16:24:41浏览次数:48  
标签:console log min max floor 汇总 js Math

// ----------  JavaScript Math  ----------

// 

// abs(x)
// 返回x的绝对值
// Math.abs(x)
console.log( Math.abs(8.8) ); // 8.8
console.log( Math.abs(-8.8) ); // 8.8
console.log( Math.abs(null) ); // 0
console.log( Math.abs(undefined) ); // NaN
console.log( Math.abs('121') ); // 121
console.log( Math.abs(6 + 6) ); // 12
console.log( Math.abs(NaN) ); // NaN

// ceil(x)
// 进行向上舍入
//  Math.ceil(x)
console.log( Math.ceil(0.9) ); // 1
console.log( Math.ceil(1.6) ); // 2
console.log( Math.ceil(16.6) ); // 17
console.log( Math.ceil(-1.6) ); // -1
console.log( Math.ceil(-16.6) ); // -16

// floor(x)
// 进行向下舍入, 返回小于等于x的最大整数
// Math.floor(x)
console.log( Math.floor(0.9) ); // 0
console.log( Math.floor(1.6) ); // 1
console.log( Math.floor(16.6) ); // 16
console.log( Math.floor(-1.6) ); // -2
console.log( Math.floor(-16.6) ); // -17

// max(x,y,z,...,n)
//     返回 x,y,z,...,n 中的最高值
// Math.max(n1,n2,n3,...,nX)
console.log( Math.max(1, 2, 3, 4, 4, 5, 6, 7, 8) ) // 8
console.log( Math.max(-1, -2, -3, -4, -4, -5, -6, -7, -8) ) // -1

// min(x,y,z,...,n)
// 返回 x,y,z,...,n中的最低值
// Math.min(n1,n2,n3,...,nX)
console.log( Math.min(1, 2, 3, 4, 4, 5, 6, 7, 8) ) // 1
console.log( Math.min(-1, -2, -3, -4, -4, -5, -6, -7, -8) ) // -8


// pow(x,y)
// 返回 x 的 y 次幂
// Math.pow(x,y)
console.log(Math.pow(2, 3)) // 8
console.log(Math.pow(1, 30000)) // 1
console.log(Math.pow(-3, 3)) // -27


// random()
// 返回 0(含) ~ 1(不含) 之间的随机数
// Math.random()
console.log( Math.random() ) // 0.29444001804261943 (随机)
console.log( Math.floor( ( Math.random() * 100 ) + 1) ) // 1 - 100随机数

  // 返回返回 min(含) - max(不含) 之间的书任意数
function getRndInteger(min, max) {
  return Math.floor( Math.random() * (max - min) ) + min;
}
console.log( getRndInteger(5, 10) ) // 5

  // 返回返回 min(含) - max(含) 之间的书任意数
function getRndInteger(min, max) {
  return Math.floor( Math.random() * (max - min + 1) ) + min;
}
console.log( getRndInteger(5, 10) ) // 9


// round(x)
// 四舍五入, 舍入最接近的数
// Math.round(x)
console.log( Math.round(2.3) ) // 2
console.log( Math.round(2.5) ) // 3
console.log( Math.round(0) ) // 0
console.log( Math.round(-2.3) ) // -2
console.log( Math.round(-2.5) ) // -2
console.log( Math.round(-2.6) ) // -3


// sqrt(x)
// 返回数的平方根
// Math.sqrt(x)
console.log( Math.sqrt(4) ) // 2
console.log( Math.sqrt(9) ) // 3
console.log( Math.sqrt(16) ) // 4
console.log( Math.sqrt(-9) ) // NaN

 

标签:console,log,min,max,floor,汇总,js,Math
From: https://www.cnblogs.com/-roc/p/17296560.html

相关文章

  • js Date汇总
    //----------JavaScriptDate----------//Date方法:常用分三大类/*get*:getFullYeargetMonthgetDategetHoursgetMinutesgetSecondsgetDaygetTime*//*set*setFullYearsetMonthsetDatesetHourssetMinutessetSecondssetTime*/......
  • js String汇总
    //----------JavaScriptString----------//charAtcharCodeAt//concatrepeatsplit//slicesubstring//replacereplaceAll//toLowerCasetoUpperCase//startsWithendsWith//indexOflastIndexOfincludes//matchsearch//charAt()//返回在指定位......
  • js RegExp汇总
    //----------JavaScriptRegExp----------/*语法:constpatt=newRegExp(pattern,modifiers)或constpatt=/pattern/modifierspattern(模式)描述了表达式的模式modifiers(修饰符)用于指定全局匹配、区分大小写的匹配和多行匹配*///修饰符-用于执......
  • JS生成随机颜色
    //传统写法functionrandomColor1(){varr=Math.floor(Math.random()*256),g=Math.floor(Math.random()*256),b=Math.floor(Math.random()*256);return`rgb(${r},${g},${b})`;}//取巧functionrand......
  • js数据遍历几种方式
    在JavaScript中,有多种方式可以遍历数据集,下面列出了常用的几种:for循环for循环是一种常见的遍历数据集的方式,可以用于遍历数组、对象等数据类型。例如:constarr=[1,2,3];for(leti=0;i<arr.length;i++){console.log(arr[i]);}constobj={a:1,b:2,......
  • h5 - pc 使用 pdf.js 预览pdf -配合文件流实现 - 遇到的坑总结
    1.pdf.js下载看我这篇随笔【h5-使用pdf.js预览pdf-岑惜-博客园(cnblogs.com)】2.html调用页面的局部代码<body><divstyle="height:100vh;margin:0auto"><iframestyle="height:100%;width:100%;border:none"id="fvic"src="&......
  • Spring Boot返回Json数据及数据封装
    1.1简介在项目开发中,接口与接口之间,前后端之间数据的传输都使用Json格式,在SpringBoot中,接口返回Json格式的数据很简单,在Controller中使用@RestController注解即可返回Json格式的数据,@RestController也是SpringBoot新增的一个注解,我们点进去看一下该注解都包含了哪些东西......
  • 小程序开发 JSON转换的使用
    前言  此篇博客讲解小程序的JSON使用,因为JavaScript与JSON泛用性太大。初学者很容易在JSON的使用上困惑。 字符串转JSON请注意,下面的字符串json是带引号的。jsonTest(){letjsonString="{\"id\":\"1\",\"name\":\"测试JSON\"}";letdata......
  • JS 字符串特殊字符全部替换空
    1、方法constformatStr=(str)=>{constvalue=str.replace(/[`:_~!@#$%^&*()\+=<>?"{}|,\/;'\\[\]·~!@#¥%……&*()——\+={}|《》?:“”【】、;‘’,。、-]/g,'',)returnvalue}2、实例......
  • [记录]php url传参json json_decode 后 null
    从APP端或从其他页面post,get过来的数据一般因为数组形式。因为数组形式不易传输,所以一般都会转json后再发送。本以为发送方json_encode(),接收方json_decode(),就解决的问题,结果发现,json_decode()后是NULL。一般会反应是少了一个参数“true”,但是回去看就是 json_decode($data,tru......