繁体   English   中英

如何在javascript中将大的负科学记数字转换为十进制记号字符串?

[英]How to convert big negative scientific notation number into decimal notation string in javascript?

converter_scientific_notation_to_decimal_notation('1.34E-15')或converter_scientific_notation_to_decimal_notation(1.34E-15)

>'0.00000000000000134'

converter_scientific_notation_to_decimal_notation('2.54E-20')或converter_scientific_notation_to_decimal_notation(2.54E-20)

>'0.000000000000000000254'

Javascript中是否存在此类函数?

parseFloat不适合大的负面科学数字。

parseFloat('1.34E-5') => 0.0000134
parseFloat('1.34E-15') => 1.34e-15

这适用于具有指数“E”,正或负的任何正或负数。 (您可以通过前缀'+'将数字字符串转换为数字,或者将其作为字符串方法或任何对象的方法,并调用字符串或数字。)

Number.prototype.noExponents= function(){
    var data= String(this).split(/[eE]/);
    if(data.length== 1) return data[0]; 

    var  z= '', sign= this<0? '-':'',
    str= data[0].replace('.', ''),
    mag= Number(data[1])+ 1;

    if(mag<0){
        z= sign + '0.';
        while(mag++) z += '0';
        return z + str.replace(/^\-/,'');
    }
    mag -= str.length;  
    while(mag--) z += '0';
    return str + z;
}

var n=2.54E-20;
n.noExponents();

返回值:

"0.0000000000000000000254"

您可以使用toFixed: (1.34E-15).toFixed(18)返回0.000000000000001340

您可以使用from-exponential模块。 它重量轻,经过全面测试。

它接受字符串和数字,因此在转换过程中不会丢失精度。

import fromExponential from 'from-exponential';

fromExponential('1.12345678901234567890e-10'); 
// '0.00000000011234567890123456789'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM