zoukankan      html  css  js  c++  java
  • Math

     Math对象上的属性都是常量,不是变量,不能被修改
     
    常用MathAPI
     1   <script>
     2     // abs 取绝对值
     3     console.log(Math.abs(4))  // 4
     4     console.log(Math.abs(-4)) // 4
     5 
     6     // ceil向上取整
     7     console.log(Math.ceil(3.7)) // 4
     8     console.log(Math.ceil(3.00001)) // 4
     9     console.log(Math.ceil(-3.00001)) // -3
    10 
    11     // floor向下取整
    12     console.log(Math.floor(3.7)) // 3
    13     console.log(Math.floor(3.00001)) // 3
    14     console.log(Math.floor(-3.00001)) // -4
    15 
    16     // round 四舍五入
    17     console.log(Math.round(3.6)) // 4
    18     console.log(Math.round(3.3)) // 3
    19     console.log(Math.round(-3.6)) // -4
    20     console.log(Math.round(-3.3)) // -3
    21     console.log(Math.round(-3.5)) // -3
    22 
    23   </script>

    不常用MathAPI

      <script>
        // 三角函数参数值传弧度而不是角度
        // 一个完整的圆角度是360度,弧度是2PI
        // 360角度 === 2PI弧度
        console.log(Math.sin(Math.PI / 6))
        // cos tan
    
        // max 拿到最大值
        console.log(Math.max(4,8,5,12))
        // min 拿到最小值
        console.log(Math.min(4,8, -4, 5))
    
        console.log(Math.pow(2, 3)) // 2的3次方 8
    
        console.log(Math.sqrt(16)) // 16的算数平方根,4
        console.log(Math.sqrt(15)) // 15的算数平方根
    
      </script>

    随机数random

     1   <script>
     2     // 这个方法没有参数,直接返回0~1的随机小数
     3     console.log(Math.random())
     4 
     5     var num = Math.ceil(Math.random() * 10) // 1-10
     6     console.log(num)
     7 
     8     var num1 = Math.floor(Math.random() * 10) // 0-9
     9     console.log(num1)
    10 
    11     // 虽然这样可以0-10,但是0和10出现的机率比其他数字小一半
    12     // 所以一般随机数不会用四舍五入
    13     // var num2 = Math.round(Math.random() * 10) // 0-10
    14     // console.log(num2)
    15 
    16     // 50~100随机数, 包含50不包含100
    17     console.log(Math.floor(Math.random() * 50) + 50)
    18     
    19   </script>
  • 相关阅读:
    WPF ObservableCollection,INotifyPropertyChanged
    WPF MainWindow的TopMost,Resizemode
    WPF WebBrowser抑制Suppress 弹出 脚本错误 对话框 但是样式改变 需要继续改善
    WPF MultiBinding,多值转化器IMultiValueConverter,自制调色板 palette
    WPF Slider设置整数
    ABAP-Generate dynpro动态屏幕
    ABAP-Generate subroutine
    ABAP-索引
    Java IO/NIO
    微分中值定理--小笔记
  • 原文地址:https://www.cnblogs.com/strongerPian/p/12716464.html
Copyright © 2011-2022 走看看