Reputation: 956
converter_scientific_notation_to_decimal_notation('1.34E-15') or converter_scientific_notation_to_decimal_notation(1.34E-15)
=> '0.00000000000000134'
converter_scientific_notation_to_decimal_notation('2.54E-20') or converter_scientific_notation_to_decimal_notation(2.54E-20)
=> '0.000000000000000000254'
Does such function exist in Javascript?
parseFloat is not good for big negative scientific number.
parseFloat('1.34E-5') => 0.0000134
parseFloat('1.34E-15') => 1.34e-15
Upvotes: 7
Views: 8610
Reputation: 429
You can use from-exponential module. It is lightweight and fully tested.
It accepts strings and numbers so it will not lose precision during conversion.
import fromExponential from 'from-exponential';
fromExponential('1.12345678901234567890e-10');
// '0.00000000011234567890123456789'
Upvotes: 0
Reputation: 104760
This works for any positive or negative number with the exponential 'E', positive or negative. (You can convert a numerical string to a number by prefixing '+', or make it a string method, or a method of any object, and call the string or number.)
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();
returned value:
"0.0000000000000000000254"
Upvotes: 17
Reputation: 6075
You can use toFixed: (1.34E-15).toFixed(18)
returns 0.000000000000001340
Upvotes: 2