Skip to main content

Bug Tracker

Side navigation

#5288 closed enhancement (wontfix)

Opened September 24, 2009 11:01AM UTC

Closed October 06, 2010 01:29AM UTC

Last modified September 19, 2012 02:14PM UTC

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;
    }

Attachments (0)
Change History (6)

Changed September 24, 2009 11:02AM UTC by andre1 comment:1

"onFalse" should have been "onNaN"

Changed November 16, 2009 12:24AM UTC by umbrae comment:2

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

Changed March 31, 2010 03:12AM UTC by mark_story comment:3

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.

Changed October 06, 2010 01:29AM UTC by snover comment:4

resolution: → wontfix
status: newclosed

This is not appropriate for jQuery core.

Changed September 19, 2012 02:05PM UTC by anonymous comment:5

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);

Changed September 19, 2012 02:14PM UTC by ajpiano comment:6

I fail to see how

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

is a preferable form of "litter."