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  
  • 相关阅读:
    Samba 4.0 RC3 发布
    SymmetricDS 3.1.7 发布,数据同步和复制
    Express.js 3.0 发布,Node.js 的高性能封装
    GIFLIB 5.0.1 发布,C语言的GIF处理库
    jQuery UI 1.9.1 发布
    SVN Access Manager 0.5.5.14 发布 SVN 管理工具
    DynamicReports 3.0.3 发布 Java 报表工具
    HttpComponents HttpClient 4.2.2 GA 发布
    AppCan 2.0 正式发布,推移动应用云服务
    Ruby 2.0 的新功能已经冻结
  • 原文地址:https://www.cnblogs.com/Anlycp/p/2279401.html
Copyright © 2011-2022 走看看