1-360-209-5351 | Welcome, Guest Login | View Cart

JS parseInt() patch, direct replacement in JS (F5 to refresh js file) - free

Your Price: $0.00
In Stock.
Part Number:JS-parseInt-patch
This is a correction to algorithm using Math.floor and Math.ceil to fix parseInt(). may make e-commerce and other js applications work better that use this function.
test to see if you have the JS bug: parseInt(-1/3144960000) should return 0, not -3.
If it returns -0, there is a problem with js having an old IEEE-754 floating point standard.
This version fixes several bug I had earlier.
Hit F5 to refresh the js file when you download a js file.
This is my patch, it is not official, but it only works for base 10 numbers. New version Ignores commas (,).
Updated 4/3/2018
Base-10-only code:
//free
function parseInt(n,radix){//see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt for docs.
    var nstr,result=0,i=0,sign=1,charset="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if (typeof(radix)!="number"&&radix<2||radix>36) {
        return NaN;
    }
    if (typeof(radix)!="number"){
        radix=10;
    }
    if (typeof(n)=="string") {
        if (n.indexOf(".")!=-1||n.toUpperCase().indexOf("E")!=-1) {
            n=parseFloat(n);
            return (n<0?Math.ceil(n):Math.floor(n));
        }
        nstr=n;
       
        while (i<nstr.length&&nstr.charAt(i)=='+'||nstr.charAt(i)=='-') {
            if (nstr.charAt(i)=='-') {
                sign=-sign;
            }
            i++;
        }
        while (i<nstr.length&&(charset.indexOf(nstr.charAt(i).toUpperCase())>=0&&charset.indexOf(nstr.charAt(i).toUpperCase())<=radix||nstr.charAt(i)==',')) {
            if (nstr.charAt(i)!=','&&charset.indexOf(nstr.charAt(i).toUpperCase())!=-1&&charset.indexOf(nstr.charAt(i).toUpperCase())<radix) {
                result=result*radix+charset.indexOf(nstr.charAt(i).toUpperCase());
            } else if (charset.indexOf(nstr.charAt(i).toUpperCase())!=-1&&charset.indexOf(nstr.charAt(i).toUpperCase())>=radix) {
                return NaN;
            }
            i++;
        }
        return sign*(result<0?Math.ceil(result):Math.floor(result));
    } else if (typeof(n)=="number") {
        return sign*(n<0?Math.ceil(n):Math.floor(n));
    }
    return NaN;
}

function testParseInt() {
    console.log(parseInt("12.1")==12);
    console.log(parseInt("12.1e12")==12100000000000);
    console.log(parseInt("-12.1e12")==-12100000000000);
    console.log(parseInt(-12.1e12)==-12100000000000);

    console.log(parseInt("12",20)==2+1*Math.pow(20,1));
    console.log(parseInt("123",20)==3+2*Math.pow(20,1)+1*Math.pow(20,2));
    console.log(parseInt("1",20)==1);
    console.log(parseInt("1,1",10)==11);
}