How to deal with Number precision in Actionscript?

后端 未结 14 1185
自闭症患者
自闭症患者 2020-11-29 03:23

I have BigDecimal objects serialized with BlazeDS to Actionscript. Once they hit Actionscript as Number objects, they have values like:

140475.32 turns

相关标签:
14条回答
  • 2020-11-29 04:06

    You can use property: rounding = "nearest"

    In NumberFormatter, rounding have 4 values which you can choice: rounding="none|up|down|nearest". I think with your situation, you can chose rounding = "nearest".

    -- chary --

    0 讨论(0)
  • 2020-11-29 04:07

    If you know the precision you need beforehand, you could store the numbers scaled so that the smallest amount you need is a whole value. For example, store the numbers as cents rather than dollars.

    If that's not an option, how about something like this:

    function printTwoDecimals(x)
    {
       printWithNoDecimals(x);
       print(".");
       var scaled = Math.round(x * 100);
       printWithNoDecimals(scaled % 100);
    }
    

    (With however you print with no decimals stuck in there.)

    This won't work for really big numbers, though, because you can still lose precision.

    0 讨论(0)
  • 2020-11-29 04:09

    I converted the Java of BigDecimal to ActionScript. We had no choices since we compute for financial application.

    http://code.google.com/p/bigdecimal/

    0 讨论(0)
  • 2020-11-29 04:10

    It seems more like a transport problem, the number being correct but the scale ignored. If the number has to be stored as a BigDecimal on the server you may want to convert it server side to a less ambiguous format (Number, Double, Float) before sending it.

    0 讨论(0)
  • 2020-11-29 04:14

    This is my generic solution for the problem (I have blogged about this here):

    var toFixed:Function = function(number:Number, factor:int) {
      return Math.round(number * factor)/factor;
    }
    

    For example:

    trace(toFixed(0.12345678, 10)); //0.1
    
    • Multiply 0.12345678 by 10; that gives us 1.2345678.
    • When we round 1.2345678, we get 1.0,
    • and finally, 1.0 divided by 10 equals 0.1.

    Another example:

    trace(toFixed(1.7302394309234435, 10000)); //1.7302
    
    • Multiply 1.7302394309234435 by 10000; that gives us 17302.394309234435.
    • When we round 17302.394309234435 we get 17302,
    • and finally, 17302 divided by 10000 equals 1.7302.


    Edit

    Based on the anonymous answer below, there is a nice simplification for the parameter on the method that makes the precision much more intuitive. e.g:

    var setPrecision:Function = function(number:Number, precision:int) {
     precision = Math.pow(10, precision);
     return Math.round(number * precision)/precision;
    }
    
    var number:Number = 10.98813311;
    trace(setPrecision(number,1)); //Result is 10.9
    trace(setPrecision(number,2)); //Result is 10.98
    trace(setPrecision(number,3)); //Result is 10.988 and so on
    

    N.B. I added this here just in case anyone sees this as the answer and doesn't scroll down...

    0 讨论(0)
  • 2020-11-29 04:17

    guys, just check the solution:

                protected function button1_clickHandler(event:MouseEvent):void
                {
                    var formatter:NumberFormatter = new NumberFormatter();
                    formatter.precision = 2;
                    formatter.rounding = NumberBaseRoundType.NEAREST;
                    var a:Number = 14.31999999999998;
    
                    trace(formatter.format(a)); //14.32
                }
    
    0 讨论(0)
提交回复
热议问题