Convert scientific notation to decimal

Asked

Viewed 3,374 times

0

How can I convert a number into scientific notation like 2.6274846602703e-6 into a decimal number using javascript only ?

3 answers

3


Just do the conversion with the function Number() that will serve any exponent within the limits defined in Number(). Behold:

var notacao_cientifica = 2.6274846602703e-6;
var string_notacao_cientifica = "2.6274846602703e-6";

var convertido_1 = Number( notacao_cientifica );
console.log( convertido_1 );

var convertido_2 = Number( string_notacao_cientifica );
console.log( convertido_2 )

One reply plus complete, valid for any exponent value, is the one that is commented below. But note that the result is string and to do basic mathematical operations you will need to convert the string to number. And in doing so, if you exceed the maximum limit set for language-defined fractional numbers, there will be value approximation and even truncation.

To perform such basic mathematical operations in numbers larger than 19 and smaller than -5 without losing precision, it would be necessary to define them manually. It’s not something easy to process with Javascript, escaping the scope of the language and the question itself. I recommend trying it just for learning.

To simply show as string, there are no problems. Works perfectly.

// Glossário
//
// NNC = Número em Notação Científica
// NCE = notação científica de mantissa E
// BASE = obtido de base()
// expoente() = obtém o expoente do número em NCE
// base() = obtém a base do número em NCE
// inteiro() = obtém parte não fracionária da base
// fracao = obtém parte fracionária da base (em números decimais)
// N = número em NCE

// Valores de exemplo
var a = "2.6274846602703e21";
var b = "2.6274846602703e-7";

// Funções de auxílio
var expoente = function(NNC) {
  return (/[e][-0-9]+$/g.exec(NNC)).toString().slice(1);
};
var base = function(NNC) {
  return (/^\d\.\d+/g.exec(NNC)).toString();
};
var inteiro = function(BASE) {
  return base(BASE).slice(0, 1);
};
var fracao = function(BASE) {
  return base(BASE).slice(2);
};

// Converte números em notação científica de mantissa "E" (E = 10) em forma decimal
var conversor_de_nce = function(N) {

  // Declaração de variáveis
  var delta, R;

  // Obtém dados necessários
  var e = Number(expoente(N));
  var b = base(N);
  var i = inteiro(b);
  var f = fracao(b);

  // Caso de expoentes positivos
  if (e >= 0) {

    // Diferença de dígitos entre a parte fracionária e o valor do expoente
    delta = f.length - e;

    return delta > 0 ? i + f.slice(0, e) + "." + f.slice(e) : i + f + "0".repeat(Math.abs(delta));

    // Caso de expoentes negativos
  } else {
    return "0." + "0".repeat(Math.abs(e) - i.length) + i + f;
  }

};


// Exemplos
console.log("número: \t\t" + a);
console.log("conversor_de_nce: \t" + conversor_de_nce(a));

console.log("---");

console.log("número: \t\t" + b);
console.log("conversor_de_nce: \t" + conversor_de_nce(b));

  • 1

    I disagree, suppose the number is: "2.6274846602703e-20" What will be returned: "2.6274846602703e-20"

  • 1

    @Marcelojunior In fact, I made a mistake. I must change with a more appropriate response and make explicit that any exponent is within the maximum allowed.

2

You can also use the sign + in front of your string to get a number.

console.log(+2.6274846602703e-6);
console.log(+"2.6274846602703e-6");

2

For exponents not too small or too large, a simple value assignment already solves your problem

let num = 2.6274846602703e-6
console.log(num)
/* 0.0000026274846602703 */

Browser other questions tagged

You are not signed in. Login or sign up in order to post.