开发者

Converting/expressing double number in non-exponent/short form in Javascript

I have a double in Javascript whose value is, for example, 1.0883076389305e-311. I want to express it in the following form, using as example the 'bc' utility to calculate the expanded/higher precision/scale form:

$ bc
scale=400
1.0883076389305000*10^-311
.0000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000000000000000000000108830763893050000000000开发者_运维知识库00000\
0000000000000000000000000000000000000000000000000000000000000

I need a Javascript bigint library or code to produce the same output as a string with the expanded/higher precision form of the number.

Thanks!


This is horrible, but works with every test case I can think of:

Number.prototype.toFullFixed = function() {
    var s = Math.abs(this).toExponential();
    var a = s.split('e');
    var f = a[0].replace('.', '');
    var d = f.length;
    var e = parseInt(a[1], 10);
    var n = Math.abs(e);

    if (e >= 0) {
        n = n - d + 1;
    }

    var z = '';
    for (var i = 0; i < n; ++i) {
        z += '0';
    }

    if (e <= 0) {
        f = z + f;
        f = f.substring(0, 1) + '.' + f.substring(1);
    } else {
        f = f + z;
        if (n < 0) {
            f = f.substring(0, e + 1) + '.' + f.substring(e + 1);
        }
    }

    if (this < 0) {
        f = '-' + f;
    }

    return f;
};

If you find a number that doesn't parse back correctly, i.e. n !== parseFloat(n.toFullFixed()), please let me know what it is!


// So long as you are dealing with strings of digits and not numbers you can use string methods to convert exponential magnitude and precision to zeroes

function longPrecision(n, p){
    if(typeof n== 'string'){
        n= n.replace('*10', '').replace('^', 'e');
    }
    p= p || 0;
    var data= String(n), mag, str, sign, z= '';
    if(!/[eE]/.test(data)){
        return data;
        if(data.indexOf('.')== -1 && data.length<p) data+= '.0';
        while(data.length<p) data+= '0';
        return data;
    }
    data= data.split(/[eE]/);
    str= data[0];
    sign= str.charAt(0)== "-"? "-": "";
    str= str.replace(/(^[+-])|\./, "");
    mag= Number(data[1])+ 1;
    if(mag < 0){
        z= sign + "0.";
        while(mag++) z += "0";
        str= z+str;
        while(str.length<p) str+= '0';
        return str;
    }
    mag -= str.length;
    str= sign+str;
    while(mag--) z += "0";
    str += z;
    if(str.indexOf('.')== -1 && str.length<p) str+= '.0';
    while(str.length<p) str+= '0';
    return str;
}


var n='1.0883076389305000*10^-311';
longPrecision(n, 400);


/*  returned value: (String)
0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001088307638930500000000000000000000000000000000000000000000000000000000000000000000000000
*/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜