-
6 Nov 2008 1:04 AM #1
[FIXED][2.x] Ext.num(NaN, 0) returns NaN
[FIXED][2.x] Ext.num(NaN, 0) returns NaN
the following code
returns NaN instead of zero. (yes, NaN is a number unfortunately).Code:Ext.num(NaN, 0);
i propose the following simple fix:
which performs type string-to-number coercion where applicable (which imho should take place. i refer to one such help request: http://extjs.com/forum/showthread.php?t=46438).Code:Ext.apply( num : function(v, defaultValue) { return Number(String(v))? Number(v) : defaultValue; } });
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
6 Nov 2008 6:56 AM #2
What about:
Code:num : function(v, defaultValue){ if(typeof v != 'number' || isNaN(v)){ return defaultValue; } return v; }
-
6 Nov 2008 8:09 AM #3
that definitely works, but doesn't take care of the following valid numeric values, simply because they're quoted and hence of type String:
p.s. wasn't the intent of Ext.num() to address the shortcomings of js's isNaN() methodCode:'1.9' '-.9' '+8.'

i.e. to check if a number could successfully be coerced into a numeric value, minus the quirks surrounding isNaN()?
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
23 Feb 2009 7:40 AM #4
I just came across this issue trying:
Expected it to return 1 (as a number). Would be great if it did the conversion - i guess it would have to decide on parseInt or parseFloat if isNaN returns false?PHP Code:Ext.num('1',0) //returns 0
Thanks
Rob
-
23 Feb 2009 9:33 AM #5
if it helps, here's what i'm currently using:
which takes care of everything i know of (including the 3 examples i posted above) -- no parseFloat / parseInt required.Code:Ext.apply(Ext, { num : function(v, defaultValue) { v = Number(v); // cast value to type Number return !isNaN(v)? v : defaultValue; } });
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
23 Feb 2009 9:35 AM #6
-
23 Feb 2009 10:03 PM #7
Modified in SVN.
Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!
-
14 Mar 2009 9:50 AM #8
found one more failing test case:
a tiny change is needed with the code in SVN to prevent this:Code:Ext.num(null); // returns 0
Code:Ext.apply(Ext, { num : function(v, defaultValue) { v = Number(v == null? NaN : v); return !isNaN(v)? v : defaultValue; } });Last edited by mystix; 14 Mar 2009 at 9:55 AM. Reason: edit
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
14 Mar 2009 7:06 PM #9
Committed.
Evan Trimboli
Sencha Developer
Twitter - @evantrimboli
Don't be afraid of the source code!


Reply With Quote
