• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

183
Vistas
How to add a output result with 4 decimals?

HTML

<div class="item main">
    <h1>Second Heading</h1>
<p>This is my second paragraph<br>
<input type="text" id="input">
  <button id="kg">Convert to Kg</button><br><br>
  <button id="pound">Convert to Pound</button>
  <input type="text" id="output">

    </div>

Javascript

  let kgbtn = document.getElementById('kg');
  let poundbtn = document.getElementById('pound');

 /*This is my converter from KG to Pound*/

 kgbtn.addEventListener('click', function() {
 let input = document.getElementById('input').value;
 document.getElementById('output').value = input / 2.205 + "kg";                    
 })

/*This is my converter from Pound to KG*/
poundbtn.addEventListener('click', function() {
let input = document.getElementById('input').value;
document.getElementById('output').value = input * 2.205 + "Pound";
})

Can anyone please help me out with adding four decimals? I'm quite new to this javascript and would appreciate some help.

almost 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use toFixed() method.

const num = 123.456789;
console.log(num.toFixed(4));
almost 3 years ago · Juan Pablo Isaza Denunciar

0

You can use toFixed() method

const num = 12345678;

const res = num.toFixed(4);

console.log(res);
// expected output = 1234,5678

function financial(x) {
  return Number.parseFloat(x).toFixed(2);
}

console.log(financial(123.456));
// expected output: "123.46"

console.log(financial(0.004));
// expected output: "0.00"

console.log(financial('1.23e+5'));
// expected output: "123000.00"

Here are examples similar to what you want

Number.prototype.toFixed()

almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda