1

I am making a weight converter. After a few people using it, some realized that it showed way too many decimals at the end. How can I make it so that it only shows up to the hundredths?

document.getElementById("output").style.visibility = "hidden";
document.getElementById("lbsInput").addEventListener("input", function(e) {
  document.getElementById("output").style.visibility = "visible";

  let lbs = e.target.value;

  document.getElementById("gramOutput").innerHTML = lbs * 454;
  document.getElementById("kgOutput").innerHTML = lbs / 2.205;
  document.getElementById("ozOutput").innerHTML = lbs * 16;
});
  • 2
    Does this answer your question? [Round to at most 2 decimal places (only if necessary)](https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary) – N'Bayramberdiyev Jan 05 '20 at 16:45

1 Answers1

0

Method 1: Use toFixed() method, it converts a number into a string, keeping a specified number of decimals.

var num = 123.456789;
var result = num.toFixed(3);

Output:

"123.456"   

Method 2: You can use Math.round() like this:

Math.round(num * 100) / 100; 

Output:

123.46
sachin soni
  • 521
  • 4
  • 7