phpjs
Version:
101 lines (85 loc) • 2.7 kB
Markdown
---
layout: page
title: "JavaScript intval function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/intval:435
- /functions/view/intval
- /functions/view/435
- /functions/intval:435
- /functions/435
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's intval
{% codeblock var/intval.js lang:js https://raw.github.com/kvz/phpjs/master/functions/var/intval.js raw on github %}
function intval (mixed_var, base) {
// From: http://phpjs.org/functions
// + 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)
// + bugfixed by: Rafał Kukawski (http://kukawski.pl)
// * 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;
} 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 mixed_var | 0;
} else {
return 0;
}
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/var/intval.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/var/intval.js)
### Example 1
This code
{% codeblock lang:js example %}
intval('Kevin van Zonneveld');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
0
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
intval(4.2);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
4
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
intval(42, 8);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
42
{% endcodeblock %}
### Other PHP functions in the var extension
{% render_partial _includes/custom/var.html %}