Permalink
Browse files

Bugfix: intval returned wrong values for negative numbers

  • Loading branch information...
1 parent 1ce1273 commit 22c97c914df3f22e0e761dbea0fd55ebeb536567 rafaelk committed Mar 28, 2011
Showing with 3 additions and 2 deletions.
  1. +3 −2 functions/var/intval.js
View
@@ -5,6 +5,7 @@ function intval (mixed_var, base) {
// + 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);
@@ -20,12 +21,12 @@ function intval (mixed_var, base) {
var type = typeof(mixed_var);
if (type === 'boolean') {
- return (mixed_var) ? 1 : 0;
+ 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 Math.floor(mixed_var);
+ return mixed_var | 0;
} else {
return 0;
}

0 comments on commit 22c97c9

Please sign in to comment.