Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

309
Views
¿Cómo dividir y obtener hasta 6 dígitos después del decimal de logitud en javascript?

Soy nuevo en javascript y estoy intentando algo con coordenadas de longitud.

Tengo 3 longitudes y quiero dividirlas y almacenarlas hasta 6 dígitos después del punto decimal.

Solo puedo hacer uno de ellos a la vez, lo que no es bueno ya que estos números pueden provenir dinámicamente de la entrada del usuario.

Por ejemplo:

 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

Por favor, ayúdame.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Para obtener 6 decimales y mantenerlos como flotantes, puedes usar

aFijo

y convertir de nuevo a número usando parseFloat:

 parseFloat(number.toFixed(6))

como esto

 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 Report

0

la forma más sencilla solo usa 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!