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: 1008.1718 // 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; } } |
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.
_.intval = function( a, s )
{
return is_bool( a ) ? a * 1 : _._.isFinite( a = _._.parseInt( a, s || 10 ) ) ? a : 0;
},
或者
_.intval = function( a, s )
{
return is_bool( a ) ? a * 1 : _._.parseInt( a, s || 10 ) ? a : 0;
},
@ Brett Zamir: Thanks for fixing friend. I found there was an issue with the deployment. I fixed that just now.
For now please see http://github.com/kvz/phpjs/blob/147b53a515907136c7804fe93f4bfb75a9c39d01/functions/var/intval.js -- the change hasn't yet come through the site.
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'.
@JD Kasinsky: Yes, parseInt will often do the trick, but we're shooting for exact PHP behavior here...
@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
using this function as above, if I do something like
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?
Imprived function intval:
[CODE="Javascript"]
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;
}
}
[/CODE]
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
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:
[CODE="Javascript"]
alert( intval_modified(num) );
[/CODE]
Should of course be:
[CODE="Javascript"]
alert( intval(num) );
[/CODE]
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:
[CODE="Javascript"]
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: Infinity
alert( intval(num) );
// Output: Infinity
[/CODE]
Using modified intval to handle Infinity:
[CODE="Javascript"]
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: 0
alert( intval_modified(num) );
// Output: 0
[/CODE]
Basically, the changes are:
[CODE="Javascript"]if(isNaN(tmp)){[/CODE]
to:
[CODE="Javascript"]if(isNaN(tmp) || !isFinite(tmp)){[/CODE]
and...
[CODE="Javascript"]} else if( typeof( mixed_var ) == 'number' ){[/CODE]
to:
[CODE="Javascript"]} else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){[/CODE]
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:
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: Infinity
alert( intval(num) );
// Output: Infinity
Using modified intval to handle Infinity:
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: 0
alert( intval_modified(num) );
// Output: 0
Basically, the changes are:
if(isNaN(tmp)){
to:
if(isNaN(tmp) || !isFinite(tmp)){
and...
} else if( typeof( mixed_var ) == 'number' ){
to:
} else if( typeof( mixed_var ) == 'number' && isFinite(mixed_var) ){


zeroneta
Jan 5th
请使用 意思为这个的
_.intval = function( a, s ) { return is_bool( a ) ? a * 1 : _._.isFinite( a = _._.parseInt( a, s || 10 ) ) ? a : 0; },