Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
How to convert my Array elements to numbers without deleting zero at the end

I want to convert my Array from this :

[ '0.500000', '0.333333', '0.166667' ]

to this :

[ 0.500000, 0.333333, 0.166667 ]

I tried +, Number, parseInt, parseFloat but none of them work.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As noted in the comments - you cannot retain the zero at the end unless it remains a string - if you convert the array values to numbers - you will lose all trailing zero's. If you need t o perform calculations - then you need to parse the strings into numbers when performing the calculation

but there is a workaround - if all the numbers have the same decimal places - you can use Math.pow() to multiply them by that power to get actual numbers.

This will convert your string array into a number array but with the numbers being mutiplied by 1 * (10 * the number of decimal places).

You can then use the values for performing calculations or whatever you need - just remember to divide by that 100000 to use them. Note that the toFixed() will convert the number to a string and provide the trailing zeros based on the number of decimal places you require.

That said - this is a hacky workaround - I would simply convert them to numbers, perform my calculation and then pad the trailing zeros with toFixed(decimalPlaces). Simple is always best.

const arr = [ '0.500000', '0.333333', '0.166667' ];

const decimalPlaces = 6;

const numArr = arr.map(x => x * (1 * Math.pow(10, decimalPlaces)));



console.log(numArr);
  // gives [500000, 333333, 166667]

const stringArr = numArr.map(y => (y / (1 * Math.pow(10, decimalPlaces))).toFixed(decimalPlaces)) 

console.log(stringArr);
  // gives ["0.500000","0.333333","0.166667"]

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda