Samuel Oliveira Posted July 19, 2016 Share Posted July 19, 2016 I managed to understand some things, but when i try to multiply 9.999 * 9.999 it gives 99.98000100000002, when it should be 99.980001, this case and any number should be precise. What's wrong in the program? Here is the code: <!DOCTYPE html><html lang="en"><head><link rel="stylesheet" type="text/css" href="ohmslaw.css"><meta charset="utf-8" /><title>Ohm's law</title></head><body><h1>Ohm's Law Calculator</h1><form action="" id="volt"><fieldset> <legend>V=R*I</legend> <p> <label for="ohm">Ohm ®</label> <input type="text" name="ohm"/> <label for="ampere">Ampere (I)</label> <input type="text" name="ampere"/> </p> <p> <input type="submit" value="Calculate"/> or <input type="reset" value="Reset"/> </p> <p> <label for="volt">Volt (V)</label> <input type="text" name="volt"/> </p></fieldset></form><script>(function () { function calculateVolt(ohm,ampere) { ohm = parseFloat(ohm); ampere = parseFloat(ampere); return (ohm*ampere); } var volt = document.getElementById("volt"); if (volt) { volt.onsubmit = function () { this.volt.value = calculateVolt(this.ohm.value, this.ampere.value); return false; }; }}());</script> Link to comment Share on other sites More sharing options...
musicman Posted July 19, 2016 Share Posted July 19, 2016 (edited) Specify the decimal number using toFixed(): var dec= 9.999 * 9.999; var specdec = dec.toFixed(6)console.log( specdec ); //99.980001 but the value will be a string. Edited July 19, 2016 by musicman Link to comment Share on other sites More sharing options...
justsomeguy Posted July 19, 2016 Share Posted July 19, 2016 That's just how it is with computers, they aren't perfect with floating-point math. Computers are binary, not base-10, so there are going to be precision errors with floating-point numbers when things get converted. There's an article about it here, which includes some links to libraries that are specifically for handling floating-point math. http://modernweb.com/2014/02/17/what-every-javascript-developer-should-know-about-floating-points/ Link to comment Share on other sites More sharing options...
Samuel Oliveira Posted July 21, 2016 Author Share Posted July 21, 2016 Musicman, where do i place this code that you adviced me, on my program? Link to comment Share on other sites More sharing options...
dsonesuk Posted July 21, 2016 Share Posted July 21, 2016 Just add '.toFixed(6)' to final part of your code that will produce the end calculated result. Link to comment Share on other sites More sharing options...
Samuel Oliveira Posted July 21, 2016 Author Share Posted July 21, 2016 It doesn't work I also tried to put "volt.toFixed(6)" and nothing. it shows the same. Link to comment Share on other sites More sharing options...
dsonesuk Posted July 21, 2016 Share Posted July 21, 2016 (edited) You are mixing id referencing, with name attribute dot notation referencing, and I suspect with some browsers they will make correct assumption what you want, but some will not. 'for' attribute value is supposed to refer to id ref of input directly following it. Example of how it should be <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" type="text/css" href="ohmslaw.css"> <meta charset="utf-8" /> <title>Ohm's law</title> </head> <body> <h1>Ohm's Law Calculator</h1> <form action="" id="voltform"> <fieldset> <legend>V=R*I</legend> <p> <label for="ohm">Ohm ®</label> <input type="text" name="ohm" id="ohm"/> <label for="ampere">Ampere (I)</label> <input type="text" name="ampere" id="ampere"/> </p> <p> <input type="submit" value="Calculate"/> or <input type="reset" value="Reset"/> </p> <p> <label for="volt">Volt (V)</label> <input type="text" id="volt" name="volt"/> </p> </fieldset> </form> <script> (function () { function calculateVolt(ohm,ampere) { ohm = parseFloat(ohm); ampere = parseFloat(ampere); return (ohm*ampere); } var voltform = document.getElementById("voltform"); var volt = document.getElementById("volt"); var ohm = document.getElementById("ohm"); var ampere = document.getElementById("ampere"); if (volt) { voltform.onsubmit = function () { volt.value = calculateVolt(ohm.value,ampere.value).toFixed(6); return false; }; } }()); </script> </body> </html> Edited July 21, 2016 by dsonesuk Link to comment Share on other sites More sharing options...
Samuel Oliveira Posted July 24, 2016 Author Share Posted July 24, 2016 In this program it happens that when i calculate volt (V) it works fine, But when i want to calculate ohm ® in the equation bellow nothing happens. I also want another equation for calculate ampere (I) in the same file. Hope you could help me here is the code: <!DOCTYPE html><html lang="en"><head><link rel="stylesheet" type="text/css" href="ohmslaw.css"><meta charset="utf-8" /><title>Ohm's law</title></head><body><h1>Ohm's Law Calculator</h1><form action="" id="voltform"><fieldset> <legend>V=R*I</legend> <p> <label for="ohm">Ohm ®</label> <input type="text" name="ohm" id="ohm"/> <label for="ampere">Ampere (I)</label> <input type="text" name="ampere" id="ampere"/> </p> <p> <input type="submit" value="Calculate"/> or <input type="reset" value="Reset"/> </p> <p> <label for="volt">Volt (V)</label> <input type="text" id="volt" name="volt"/> </p></fieldset></form><script>(function () { function calculateVolt(ohm,ampere) { ohm = parseFloat(ohm); ampere = parseFloat(ampere); return (ohm*ampere); } var voltform = document.getElementById("voltform");var volt = document.getElementById("volt");var ohm = document.getElementById("ohm");var ampere = document.getElementById("ampere"); if (volt) { voltform.onsubmit = function () { volt.value = calculateVolt(ohm.value,ampere.value).toFixed(6); return false; }; }}());</script><form action="" id="ohmform"><fieldset> <legend>R=V/I</legend> <p> <label for="volt">Volt (V)</label> <input type="text" name="volt" id="volt"/> <label for="ampere">Ampere (I)</label> <input type="text" name="ampere" id="ampere"/> </p> <p> <input type="submit" value="Calculate"/> or <input type="reset" value="Reset"/> </p> <p> <label for="ohm">Ohm ®</label> <input type="text" id="ohm" name="ohm"/> </p></fieldset></form><script>(function () { function calculateOhm(volt,ampere) { volt = parseFloat(volt); ampere = parseFloat(ampere); return (volt/ampere); } var ohmform = document.getElementById("ohmform");var ohm = document.getElementById("ohm");var volt = document.getElementById("volt");var ampere = document.getElementById("ampere"); if (ohm) { ohmform.onsubmit = function () { ohm.value = calculateOhm(volt.value,ampere.value).toFixed(6); return false; }; }}());</script></body></html> Link to comment Share on other sites More sharing options...
dsonesuk Posted July 24, 2016 Share Posted July 24, 2016 Id references must be unique within a page, by using duplicate id ref the js code will only work for one and not other/s. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now