JS数值

作者: olivia1111 | 来源:发表于2016-01-24 10:38 被阅读13次

    整型
    浮点型
    科学计数法
    十六进制

    复杂运算

    • 绝对值

    Math.abs(5);
    Math.abs(-5);

    • 四舍五入-把浮点型变为整型

    Math.round(1.1)//1
    Math.round(1.9)//2

    • 向上取整

    Math.ceil(1.1)//2
    Math.ceil(1.9)//2

    • 向下取整

    Math.floor(1.1)//1
    Math.floor(1.9)//1

    • 取最大值

    Math.max([value1[,value2[,..]]])
    Math.max(1,2)//2
    Math.max(-1,-2,-3)//-1

    • 取最小值

    Math.min([value1[,value2[,..]]])

    • 获取随机数

    Math.random()


    0=<范围<1
    • 其他运算方法


      运算方法

    数值转换

    • parseInt(string,radix)识别并转为整数
      string:字符串
      radix:进制

    parseInt(‘1.1’)//1
    parseInt(‘1.9’)//1
    parseInt(‘1b2.4’)//遇到非数字之后无法识别
    parseInt(‘abc’)//NaN,Not a number

    • parseFloat(string)识别浮点数

    parseFloat('102.1')//102.1
    parseFloat('12.4b1')//12.4
    parseFloat(‘abc’)//NaN,Not a number

    • Number(value)只能识别数字

    Number('101.1')//101.1
    Number('12.4b1')//NaN

    • (num).toFixed(digits)保留小数点后位数

    (102.123).toFixed(2)//102.12
    (102.123).toFixed(0)//102

    相关文章

      网友评论

          本文标题:JS数值

          本文链接:https://www.haomeiwen.com/subject/vkghkttx.html