-
25 Mar 2013 10:33 PM #1
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
- IE8
- Chrome
- 1.2601
- 1.26
Code:Ext.util.Format.round(1.26005,4)
-
26 Mar 2013 5:27 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,656
- Vote Rating
- 435
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.
-
26 Mar 2013 7:23 AM #3
Javascript's IEEE 754 number format is notoriously inaccurate at representing fractional values.
To round 1.25005, the first step is
Which Javascript tells us isCode:1.26005 * 10000
So when you round that, and divide back down, it ends up as 1.26Code:12600.499999999998
I will do some research on workarounds.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
26 Mar 2013 7:45 AM #4
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; }
-
26 Mar 2013 8:01 AM #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; };Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
26 Mar 2013 8:08 AM #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.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
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.


Reply With Quote