Bug Tracker

Opened 10 years ago

Closed 9 years ago

Last modified 7 years ago

#5288 closed enhancement (wontfix)

Add jQuery.num() function to cast value to number (int/float)

Reported by: andre1 Owned by:
Priority: major Milestone: 1.4
Component: core Version: 1.3.2
Keywords: Cc:
Blocked by: Blocking:

Description

Add a simple function to cast value to number, either as jQuery.num( <value>[, <onFalse=0>[, <type=float|int>]] ) or as separate functions jQuery.int( <value>[, <onFalse=0>] ) and jQuery.float( <value>[, <onFalse=0>] ).

Bellow follows example functions:

   function int(value, onFalse)
   {
       // Checks if value is int, if not onFalse or 0 is returned
       value = parseInt( value );
       return isNaN( value ) ? ( onFalse !== undefined ? onFalse : 0 ) : value;
   }
   function float(value, onFalse)
   {
       // Checks if value is float, if not onFalse or 0 is returned
       value = parseFloat( value );
       return isNaN( value ) ? ( onFalse !== undefined ? onFalse : 0 ) : value;
   }

Change History (6)

comment:1 Changed 10 years ago by andre1

"onFalse" should have been "onNaN"

comment:2 Changed 10 years ago by umbrae

Feels unnecessary to me. It's one line to do in pure JS. Why clutter the source with methods for it?

comment:3 Changed 10 years ago by mark_story

Sounds like a feature that could be easily handled by a non jQuery method. It doesn't really relate to any of the things jQuery is really good at other than its in Javascript.

comment:4 Changed 9 years ago by snover

Resolution: wontfix
Status: newclosed

This is not appropriate for jQuery core.

comment:5 Changed 7 years ago by anonymous

Feels unnecessary to me. It's one line to do in pure JS. Why clutter the source with methods for it?

This should be reconsidered. It may only be one line of pure JS, but I'd prefer to push that one line up into jQuery rather than having to litter my code with 100's of lines like this:

parseInt($('selector').val(), 10);

comment:6 Changed 7 years ago by ajpiano

I fail to see how

jQuery.num( $("selector").val(), 0, "int" );

is a preferable form of "litter."

Note: See TracTickets for help on using tickets.