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

141
Vistas
How can I get every one decimal place number in a string input

I have a string

let input = "1.0 2.5 3.0 4.5 5.0 -5.5 4.5 3.0 -2.0 -1.0 1.0 2.0 5.0";

In order to get a number array, I tried to do the following:

let output= input.split(" ").map(Number);

So now i got a number array. However, using the following code, I failed to format all numbers with one decimal place:

for (i = 0; i < output.length; i++) {
  output[i].toFixed(1);
}

let input = "1.0 2.5 3.0 4.5 5.0 -5.5 4.5 3.0 -2.0 -1.0 1.0 2.0 5.0";
let output = input.split(" ").map(Number);
console.log(output); //got a number array

for (i = 0; i < output.length; i++) {
  output[i].toFixed(2);
}

console.log(output); //try to get all numbers with one decimal place,but failed

Can anyone know how to make this? Thanks in advance.

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

0

You need an assignment or map the new values.

let input = "1.0 2.5 3.0 4.5 5.0 -5.5 4.5 3.0 -2.0 -1.0 1.0 2.0 5.0";
let output= input.split(" ").map(Number).map(v => v.toFixed(1));

console.log(output);

As mentioned in comments by Terry, you could combine the last both mappings into one.

let input = "1.0 2.5 3.0 4.5 5.0 -5.5 4.5 3.0 -2.0 -1.0 1.0 2.0 5.0";
let output= input.split(" ").map(v => Number(v).toFixed(1));

console.log(output);

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