zoukankan      html  css  js  c++  java
  • 取整、取小数点位数

    as3中的取整、取小数点位数

    Math.round()方法:

    Math.round()可以四舍五入对数字取整

    1
    2
    trace(Math.round(39.88));   //输出40
    trace(Math.round(58.33));   //输出58

    Math.floor()方法:

    Math.floor()可以向下取整

    1
    2
    trace(Math.floor(39.88));   //输出39
    trace(Math.floor(58.33));   //输出58

    Math.ceil()方法:

    Math.ceil()可以向上取整

    1
    2
    trace(Math.ceil(39.88));    //输出40
    trace(Math.ceil(58.33));    //输出59

    toFixed()方法:

    toFixed()方法四舍五入取指定位数的小数点,当其中参数为0时表示不留小数点

    1
    2
    3
    4
    5
    var temp:Number=3.1415926
    //toFixed()中的参数就是需要取的小数位数,0表示不留小数点
    trace(temp.toFixed(0));     //输出3
    temp=18.888;
    trace(temp.toFixed(0));     //输出19

    ==============================

    Math.round()方法取小数位数:

    比如说3.14159要精确到.001位,则先3.14159/.001,然后再Math.round(3.14159/.001),最后在把结果乘以需要精确的位数Math.round(3.14159/.001)*.001

    1
    2
    trace(Math.round(3.14159/.001)*.001);   //输出3.142
    trace(Math.round(3.14159/.01)*.01); //输出3.14

    如果不想四舍五入,那直接改round()方法为floor()或者ceil()方法即可

    toFixed()方法取小数位数:

    直接指定toFixed()中的参数即可,比如要留两位小数,则toFixed(2)

    1
    2
    3
    4
    var temp:Number=3.1415926
    trace(temp.toFixed(1));     //输出3.1
    temp=18.888;
    trace(temp.toFixed(2));     //输出18.89
  • 相关阅读:
    Git使用(真的有熟悉吗)
    webpack中webpack.config.js的相关配置表
    import和require(你有过疑惑么)
    webpack打包(离开脚手架,你还好吗)
    webpack再度学习
    最大子段和之分治法
    快速排序之随机快排
    棋盘覆盖问题(看完这个就好)
    稳定匹配问题(脱单就靠这波了)
    Jieba统计词频并生成csv文件
  • 原文地址:https://www.cnblogs.com/xcai/p/2729709.html
Copyright © 2011-2022 走看看