zoukankan      html  css  js  c++  java
  • Flex Nunber 四舍五入取值

    查了很多四舍五入的取值方式,悲剧了  都只介绍toFixed,但取得值好像不是四舍五入。怎么大家就没想过用round来求得呢,round取得的是整数进行四舍五入

    下面例子是取两位小数,如果取三位小数,值需要改为Math.round(a*1000)/1000  ,以此类推。。。

                    var a:Number=new Number();
                    a=1.1456
                    //保留两位小数,并且四舍五入
                    a=Math.round(a*100)/100  //a 值为1.15
                    
                    a=1.1456
                    //保留两位小数,不进行四舍五入
                    var b:String=currencyFormatter.format(a);  //a 值为1.14
                    
                    //toFixed 取两位小数的时候 是四舍五入;取一、三位小数的时候是五舍六入 好邪门,不大会用
                    a=1.14
                    a=Number(a.toFixed(1));   //a 值为1.1
                    a=1.154
                    a=Number(a.toFixed(2));   //a 值为1.15
                    a=1.1554
                    a=Number(a.toFixed(3));   //a 值为1.155
                    
                    a=1.15
                    a=Number(a.toFixed(1));   //a 值为1.1
                    a=1.155
                    a=Number(a.toFixed(2));   //a 值为1.16
                    a=1.1555
                    a=Number(a.toFixed(3));   //a 值为1.155
                    
                    a=1.16
                    a=Number(a.toFixed(1));   //a 值为1.2
                    a=1.156
                    a=Number(a.toFixed(2));   //a 值为1.16
                    a=1.1556
                    a=Number(a.toFixed(3));   //a 值为1.156

    定义下currencyFormatter 为两位小数,同时去除货币符号

        <fx:Declarations>
            <mx:CurrencyFormatter id="currencyFormatter" currencySymbol="" precision="2" />
        </fx:Declarations>

    取整各种方法 (原文引用:http://hanyi366.iteye.com/blog/1552493 )

         /**ceil   向前(数轴向右)取整(返回值为Number)**/  
            trace(Math.ceil(10.4));    //11  
            trace(Math.ceil(10.5));    //11  
            trace(Math.ceil(-10.4));    //-10  
            trace(Math.ceil(-10.5));    //-10  
               
             /**floor   向后(数轴向左)取整(返回值为Number)**/  
                trace(Math.floor(300.4));    //300  
                trace(Math.floor(300.5));    //300  
                trace(Math.floor(-300.4));    //-301  
                trace(Math.floor(-300.5));    //-301  
                  
                /**round   整数四舍五入,负数五舍六入(返回值为Number)**/  
                trace(Math.round(8000.4));    //8000  
                trace(Math.round(8000.5));    //8001  
                trace(Math.round(-8000.4));    //-8000  
                trace(Math.round(-8000.5));    //-8000  
                trace(Math.round(-8000.6));    //-8001  
                  
                /**toFixed   正负数都四舍五入(返回值为String)**/  
                trace(new Number(4).toFixed(3));    //4.000  
                trace(new Number(3.85742).toFixed(3));    //3.857  
                trace(new Number(3.85752).toFixed(3));    //3.858  
                trace(new Number(-3.85742).toFixed(3));    //-3.857  
                trace(new Number(-3.85752).toFixed(3));    //-3.858  
  • 相关阅读:
    web 移动端 适配
    meta
    meta设置
    时间
    CentOS下配置nginx conf/koi-win为同一文件的各类错误
    CentOS7 配置LAMP
    centos 进度条卡死
    LeetCode02:两数相加
    LeetCode01:两数之和
    单链表类,链表逆置
  • 原文地址:https://www.cnblogs.com/Anlycp/p/2279401.html
Copyright © 2011-2022 走看看