Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

313
Visualizações
How to split and get till 6 digit after the decimal of logitude in javascript?

I am new to javascript and trying something with longitude coordinate.

I have 3 longitudes and I want to split and store them till 6 digits after the decimal point

I am able to do only one of them at a time which is not good as these numbers can come dynamically from user input.

For example:

let long1 = 151.21484501290186; // I want to store till 151.214845
let long2 = 77.55814612714227;  // I want to store till 77.558146
let long3 = -122.0898574222976; // I want to store till -122.089857

// this method only works if starting value is of 2 digits but failed when it's 3
const result = long2.toString().substr(long2.toString().indexOf('.') - 2, 9);
console.log(result) // 77.558146

Please help me.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

To get 6 decimals and keep as float, you can use

toFixed

and convert back to number using parseFloat:

parseFloat(number.toFixed(6))

like this

let longs = [151.21484501290186, // I want to store till 151.214845
 77.55814612714227,  // I want to store till 77.558146
 -122.0898574222976 // I want to store till -122.089857
 ]
 
longs =  longs.map(long => parseFloat(long.toFixed(6)))
console.log(longs)


// or just one of them
const long = 151.21484501290186;
const shorter = parseFloat(long.toFixed(6))
console.log(shorter)

about 4 years ago · Juan Pablo Isaza Relatório

0

simplest way just use toFixed()

let long1 = 151.21484501290186; // I want to store till 151.214845
let long2 = 77.55814612714227;  // I want to store till 77.558146
let long3 = -122.0898574222976; // I want to store till -122.089857

// this method only works if starting value is of 2 digits but failed when it's 3
const result = long2.toString().substr(long2.toString().indexOf('.') - 2, 9);

const result1 = long1.toFixed(6)
console.log(result) // 77.558146
console.log(result1)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda