You found a bug! We've classified it as EXTJS-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
    Gainesville, FL
    Posts
    37,652
    Vote Rating
    901
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Thanks for the report.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 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,546
    Vote Rating
    62
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      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 Premium Member
    Join Date
    Nov 2011
    Posts
    72
    Vote Rating
    13
    eric.cook will become famous soon enough

      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,546
    Vote Rating
    62
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      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,546
    Vote Rating
    62
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      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.