You found a bug! We've classified it as EXTJS-10752 . We encourage you to continue the discussion and to find an acceptable workaround while we work on a permanent fix.
  1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    25
    Vote Rating
    4
    caleywoods is on a distinguished road

      1  

    Default Ext.util.Format.usMoney behaves inappropriately

    Ext.util.Format.usMoney behaves inappropriately


    REQUIRED INFORMATION

    Ext version tested:
    • Ext 4.1.1.1
    Browser versions tested against:
    • Chrome 30.0.1573.2 canary
    DOCTYPE tested against:
    • html
    Description:
    • usMoney method passes off to Ext.util.Format.currency which passes off to Ext.util.Format.number. The number method performs rounding based on the format accepted as its second argument. The problem is that it deals with floating point math and this is sometimes not accurate enough.
    Steps to reproduce the problem:
    • In chrome console, try Ext.util.Format.number(20.775, "0.00")
    • Now try Ext.util.Format.number(20.776, "0.00")
    The result that was expected:
    • In both cases stated above the expected result is "20.78", whether the round method is ROUND_HALF_UP or ROUND_HALF_EVEN it should go to "20.78"
    The result that occurs instead:
    • The result is that the example in the first bullet for reproduction (formatting 20.775) results in "20.77". This occurs because the number format method calls Ext.Number.toFixed() which looks to be an approximation of Javascripts built in toFixed() method. The root cause of this is that it deals with floating point which is represented in base2 (binary) and 20.775 is not able to be represented in binary accurately enough for the round to work correctly (ends up around 20.77499999 and so on).
    Test Case:
    Code:
        
    Ext.util.Format.number(20.775, "0.00"); // returns "20.77" expected "20.78"
    Ext.util.Format.number(20.776, "0.00"); // returns "20.78" which is expected

    HELPFUL INFORMATION

    Screenshot or Video:
    Possible fix:
    • My personal fix involved pulling in a JS library known as Big.js and use a custom renderer where I need it (numberfields in my grids) to set the value of 20.775 to $20.78 in my grid. Sencha could do something similar in building something like Big.js internally and making the usMoney method pass to this arbitrary decimal precision library. I don't feel like it's appropriate to leave currency formatting up to floating point math.
    Additional CSS used:
    • only default ext-all.css
    Operating System:
    • Win 7 Enterprise x64
    Last edited by caleywoods; 24 Jul 2013 at 12:31 PM. Reason: version info added

  2. #2
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    6,062
    Vote Rating
    215
    slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of

      0  

    Default


    Thanks for the report! I have opened a bug in our bug tracker.

  3. #3
    Sencha User
    Join Date
    Feb 2012
    Posts
    25
    Vote Rating
    4
    caleywoods is on a distinguished road

      0  

    Default


    Any updates on this?

Thread Participants: 1