Ticket #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: | ||
| 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: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);
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

"onFalse" should have been "onNaN"