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

58
Views
Escribe un número decimal en forma corta.
let number = 0.0000000123456

Quiero este number como 0.000000123 . Básicamente, quiero 3 caracteres después del último 0 . Los números de ceros no son constantes y pueden variar y la longitud de los números después de los ceros también puede variar.

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

0

La forma en que lo haría sería

  • Transformar el número en una cadena (usando, por ejemplo, esta respuesta de StackOverflow )
  • Dividir el número con 0 para obtener el número de 0 después de la coma (que es la longitud de la matriz menos 2)
  • Arreglando el número con el método toFixed con el número de 0 después de la coma + 3

 function toPlainString(num) { return ('' + +num).replace(/(-?)(\d*)\.?(\d*)e([+-]\d+)/, function(a, b, c, d, e) { return e < 0 ? b + '0.' + Array(1 - e - c.length).join(0) + c + d : b + c + d + Array(e - d.length + 1).join(0); }); } function toFixedAfterZeros(number, length) { const numberToString = toPlainString(number) const nbZeros = numberToString.split('0') const nbZerosAfterComa = nbZeros.length - 2 > 0 ? nbZeros.length - 2 : 0 return number.toFixed(nbZerosAfterComa + 3) } console.log(toFixedAfterZeros(0.00000021612621, 3)) console.log(toFixedAfterZeros(0.00000021, 3)) console.log(toFixedAfterZeros(1.1, 3)) console.log(toFixedAfterZeros(210, 3)) console.log(toFixedAfterZeros(-12, 3))

Usando una línea: number.toFixed(toPlainString(number).split('0').length - 2 + 3)

about 4 years ago · Juan Pablo Isaza Report

0

 function makeIt(n){ return n.toFixed(20).match(/^-?\d*\.?0*\d{0,3}/)[0]; } console.log(makeIt(0.00000041123215)); console.log(makeIt(0.23215)); console.log(makeIt(-0.00023215)); console.log(makeIt(500));

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!