在 Javascript 中以非指数/短形式转换/表达双数

发布于 2024-11-24 23:58:18 字数 659 浏览 1 评论 0原文

我在 Javascript 中有一个双精度值,例如,其值为 1.0883076389305e-311。 我想用以下形式表达它,例如使用 'bc' 实用程序来计算扩展/更高精度/比例形式:

$ bc
scale=400
1.0883076389305000*10^-311
.0000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000010883076389305000000000000000\
0000000000000000000000000000000000000000000000000000000000000

我需要一个 Javascript bigint 库或代码来生成与具有扩展/更高精度的字符串相同的输出数字的精度形式。

谢谢!

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\
00000000000000000000000000000000000000010883076389305000000000000000\
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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗷兮 2024-12-01 23:58:18

这很可怕,但适用于我能想到的每个测试用例:

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;
};

如果您发现一个无法正确解析的数字,即 n !== parseFloat(n.toFullFixed()),请让我知道它是什么!

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!

乙白 2024-12-01 23:58:18

// 只要你处理的是数字字符串而不是数字,你就可以
使用字符串方法将指数大小和精度转换为零

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
*/

// 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
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文