You found a bug! We've classified it as EXTJSIV-9257 . 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
    Jun 2012
    Location
    China
    Posts
    10
    Vote Rating
    1
    lutiayi is on a distinguished road

      0  

    Default Ext.util.Format.round doen't work properly

    Ext.util.Format.round doen't work properly


    REQUIRED INFORMATIONExt version tested:
    • Ext 4.2.0.663
    Browser versions tested against:
    • IE8
    • Chrome
    The result that was expected:
    • 1.2601
    The result that occurs instead:
    • 1.26
    Test Case:
    Code:
        Ext.util.Format.round(1.26005,4)

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,656
    Vote Rating
    435
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    Thanks for the report.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,458
    Vote Rating
    20
    Animal is a jewel in the rough Animal is a jewel in the rough Animal is a jewel in the rough

      0  

    Default


    Javascript's IEEE 754 number format is notoriously inaccurate at representing fractional values.

    To round 1.25005, the first step is

    Code:
    1.26005 * 10000
    Which Javascript tells us is

    Code:
    12600.499999999998
    So when you round that, and divide back down, it ends up as 1.26

    I will do some research on workarounds.

  4. #4
    Sencha User
    Join Date
    Nov 2011
    Posts
    16
    Vote Rating
    0
    eric.cook is on a distinguished road

      0  

    Default Rounding without Math.round

    Rounding without Math.round


    I've never liked using Math.round. The following function works for 1.26005.

    Code:
    function betterRound(value, precision) {
       var result = Number(value),
            adjust = 0.5;
    
       if (typeof precision === 'number') {
          precision = Math.pow(10, precision);
          adjust = adjust / precision;
          result = Math.floor((value + adjust) * precision) / precision;
       }
       return result;
    }

  5. #5
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,458
    Vote Rating
    20
    Animal is a jewel in the rough Animal is a jewel in the rough Animal is a jewel in the rough

      0  

    Default


    try

    Code:
    Ext.util.Format.round = function(value, precision) {
        var result = Number(value),
            fraction = result.toString().split('.')[1],
    
        // If there is a fraction, top it up.
        // So 3.14 gets 0.004999999999 added to it to ensure no loss of data
        increment = fraction ? .4999999999 * Math.pow(10, -fraction.length) : 0;
    
        if (typeof precision == 'number') {
            precision = Math.pow(10, precision);
            result = Math.round((value + increment) * precision) / precision;
        }
        return result;
    };

  6. #6
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,458
    Vote Rating
    20
    Animal is a jewel in the rough Animal is a jewel in the rough Animal is a jewel in the rough

      0  

    Default


    Yes, probably better to ditch Math.round, and just do it in code as Eric suggested.

    I'll open a ticket with this suggestion as an improvement.