Bug Tracker

Modify

Ticket #5288 (closed enhancement: wontfix)

Opened 4 years ago

Last modified 8 months ago

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:
Blocking: Blocked by:

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

comment:1 Changed 4 years ago by andre1

"onFalse" should have been "onNaN"

comment:2 Changed 4 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 3 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 3 years ago by snover

  • Status changed from new to closed
  • Resolution set to wontfix

This is not appropriate for jQuery core.

comment:5 Changed 8 months 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 8 months ago by ajpiano

I fail to see how

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

is a preferable form of "litter."

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.