Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript, weird floating point number endless decimal?
    text
    copied!<p>I need some help with floating point numbers...please!</p> <p>Here's the thing, I have code like below:</p> <pre><code>&lt;script&gt; function add() { document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) + parseFloat(document.getElementById("num2").value); } function subtract() { document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) - parseFloat(document.getElementById("num2").value); } function multiply() { document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) * parseFloat(document.getElementById("num2").value); } function divide() { document.getElementById("answer").value = parseFloat(document.getElementById("num1").value) / parseFloat(document.getElementById("num2").value); } &lt;/script&gt; </code></pre> <p>Sorry it's a bit long! And then the html is pretty simple:</p> <pre><code>&lt;h1&gt;Calculator&lt;/h1&gt; &lt;h3&gt;Enter a value in each box below, then click an operation&lt;/h3&gt; &lt;form id="calculatorForm"&gt; &lt;input id="num1" value="0" type="text"&gt; &lt;input id="num2" value="0" type="text"&gt; &lt;br&gt; &lt;input value="+" onclick="add();" type="button"&gt; &lt;input value="-" onclick="subtract();" type="button"&gt; &lt;input value="*" onclick="multiply();" type="button"&gt; &lt;input value="/" onclick="divide();" type="button"&gt; &lt;br&gt; &lt;input id="answer" value="0" type="text"&gt; &lt;/form&gt; </code></pre> <p>You can pretty much guess what my question is gonna be: when I multiply, divide, or subtract two floating point numbers, I end up with an infinite decimal.</p> <p>I need a quick solution that will round those numbers to two decimal points, and I need it to work later on, because I then need to implement Fahrenheit-to-Celsisus operations afterwards.</p> <p>I don't care how this is done, but it <strong>must</strong> be Javascript. Sorry if this has been answered before, but I really need an answer soon! Thanks!</p> <p>EDIT: <strong>A BIG Thankyou to the helpful people who answered my questions. Thank you!</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload