# Thread: Ext.util.Format.round doen't work properly

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. ## 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. Thanks for the report.

3. 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. ## 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),

if (typeof precision === 'number') {
precision = Math.pow(10, precision);
result = Math.floor((value + adjust) * precision) / precision;
}
return result;
}```

5. 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. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•