Use PHP functions in JavaScript

JavaScript intval

Get the integer value of a variable using the optional base for the conversion

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
35
function intval (mixed_var, base) {
    // Get the integer value of a variable using the optional base for the conversion  
    // 
    // version: 910.813
    // discuss at: http://phpjs.org/functions/intval    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Matteo
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
    // *     example 5: intval('1e', 16);
    // *     returns 5: 30    var tmp;
 
    var type = typeof( mixed_var );
 
    if (type === 'boolean') {        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
intval('Kevin van Zonneveld');

Should return

1
 

» Example 2

Running

1
intval(4.2);

Should return

1
4

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have intval goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
zeroneta
Jan 5th Permalink

q   后面那个是有问题的 没有把 a 正确 传递

请使用 意思为这个的

1
2
3
4
_.intval = function( a, s )
{
        return is_bool( a ) ? a * 1 : _._.isFinite( a = _._.parseInt( a, s || 10 ) ) ? a : 0;
},

Gravatar
zeroneta
Jan 5th Permalink

q  

1
2
3
4
_.intval = function( a, s )
{
        return is_bool( a ) ? a * 1 : _._.isFinite( a = _._.parseInt( a, s || 10 ) ) ? a : 0;
},

或者
1
2
3
4
_.intval = function( a, s )
{
        return is_bool( a ) ? a * 1 : _._.parseInt( a, s || 10 ) ? a : 0;
},

Gravatar
Kevin van Zonneveld
8 Oct '09 Permalink

q  @ Brett Zamir: Thanks for fixing friend. I found there was an issue with the deployment. I fixed that just now.

Gravatar
Brett Zamir
6 Oct '09 Permalink

q  For now please see http://github.com/kvz/phpjs/blob/147b53a515907136c7804fe93f4bfb75a9c39d01/functions/var/intval.js -- the change hasn't yet come through the site.

Gravatar
Stefan Richter
6 Oct '09 Permalink

q  I thought this function returns a number like PHP, not a string.
E.g. expected
intval('1') + 1 = 2
but the result of this is '11'.

Gravatar
Brett Zamir
17 Sep '09 Permalink

q  @JD Kasinsky: Yes, parseInt will often do the trick, but we're shooting for exact PHP behavior here...

Gravatar
Brett Zamir
17 Sep '09 Permalink

q  @Matteo: Glad you caught that. Fixed in git (also did some other clean-up). For now, see latest version at http://github.com/kvz/phpjs/commits/master/functions/var/intval.js

Gravatar
JD Kasinsky
17 Sep '09 Permalink

q   This function should work in the most cases:

1
2
3
var v='10px';
        v=parseInt(v);
        alert(v);


Bye
JD

Gravatar
Matteo
16 Sep '09 Permalink

q   using this function as above, if I do something like

1
alert(is_integer(intval('12')))


I always get FALSE while I'd expect to get TRUE. This happens 'cause if the argument is a string, you return a string and not a number. Why is so? Shouldn't intval always return a number?

Gravatar
mkl.keck
6 Mar '09 Permalink

q   Imprived function intval:

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
function intval() {
    var a = arguments;
    var v = a[0];
    var t = typeof(a[0])
    var b = ( (typeof(a[1]) !== 'undefined' && !isNaN(a[1])) ? parseInt(a[1]) : 10 );    switch (t.substring(0, 1).toLowerCase()) {
        case 'b':
            return ( (v === true) ? 1 : 0 );
        case 's':
            var r = parseInt(v * 1);            return ( (!isNaN(v) && isFinite(v)) ? r.toString(b) : 0 );
        case 'n':
            return ( isFinite(v) ? Math.floor(v) : 0 );
        default:
            return 0;    }
}

Gravatar
Kevin van Zonneveld
31 May '08 Permalink

q  @ Alex: Thank you Alex. Added!

Gravatar
Alex
30 May '08 Permalink

q  function isInt(x)
{
var y=parseInt(x);
if (isNaN(y)) return false;
return x==y && x.toString()==y.toString();
}

taken from http://community.livejournal.com/nullzone/1223.html

Gravatar
Kevin van Zonneveld
14 May '08 Permalink

q  @ stenci: Thanks a lot for your improvements stensi!

Gravatar
stensi
14 May '08 Permalink

q   Sorry for double post. I didn't give the correct code syntax highlighting first time round.

I noticed I the example I gave had an incorrect call:

1
alert( intval_modified(num) );


Should of course be:


1
alert( intval(num) );

Gravatar
stensi
14 May '08 Permalink

q   Just stumbled across your PHP.js library, awesome work Kevin :)

I think your intval could be improved a little to handle JavaScripts "Infinity" value.

for example, using current PHP.js intval:

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
function intval( mixed_var, base ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
     var tmp;
 
    if( typeof( mixed_var ) == 'string' ){
        tmp = parseInt(mixed_var);
        if(isNaN(tmp)){            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if( typeof( mixed_var ) == 'number' ){        return Math.floor(mixed_var);
    } else{
        return 0;
    }
} 
// set num to: Infinity
var num = 1 / 0;
 
// using current PHP.js intval returns: Infinityalert( intval(num) );
 
// Output: Infinity



Using modified intval to handle Infinity:
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
function intval( mixed_var, base ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
     var tmp;
 
    if( typeof( mixed_var ) == 'string' ){
        tmp = parseInt(mixed_var);
        if(isNaN(tmp) || !isFinite(tmp)){            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){        return Math.floor(mixed_var);
    } else{
        return 0;
    }
} 
// set num to: Infinity
var num = 1 / 0;
 
// using intval_modified to handle Infinity returns: 0alert( intval_modified(num) );
 
// Output: 0


Basically, the changes are:
1
if(isNaN(tmp)){

to:
1
if(isNaN(tmp) || !isFinite(tmp)){


and...

1
} else if( typeof( mixed_var ) == 'number' ){

to:
1
} else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){

Gravatar
stensi
14 May '08 Permalink

q   Just stumbled across your PHP.js library, awesome work Kevin :)

I think your intval could be improved a little to handle JavaScripts "Infinity" value better.

For example, using current PHP.js intval:

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
function intval( mixed_var, base ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
     var tmp;
 
    if( typeof( mixed_var ) == 'string' ){
        tmp = parseInt(mixed_var);
        if(isNaN(tmp)){            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if( typeof( mixed_var ) == 'number' ){        return Math.floor(mixed_var);
    } else{
        return 0;
    }
} 
// set num to: Infinity
var num = 1 / 0;
 
// using current PHP.js intval returns: Infinityalert( intval(num) );
 
// Output: Infinity



Using modified intval to handle Infinity:
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
function intval( mixed_var, base ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
     var tmp;
 
    if( typeof( mixed_var ) == 'string' ){
        tmp = parseInt(mixed_var);
        if(isNaN(tmp) || !isFinite(tmp)){            return 0;
        } else{
            return tmp.toString(base || 10);
        }
    } else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){        return Math.floor(mixed_var);
    } else{
        return 0;
    }
} 
// set num to: Infinity
var num = 1 / 0;
 
// using intval_modified to handle Infinity returns: 0alert( intval_modified(num) );
 
// Output: 0


Basically, the changes are:
1
if(isNaN(tmp)){

to:
1
if(isNaN(tmp) || !isFinite(tmp)){


and...

1
} else if( typeof( mixed_var ) == 'number' ){

to:
1
} else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){


Contribute a New function