# Help in formula on javascript

## Recommended Posts

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">
<meta charset="utf-8" />
<title>Ohm's law</title>

<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>

##### Share on other sites

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 by musicman
##### Share on other sites

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.

##### Share on other sites

Musicman, where do i place this code that you adviced me, on my program?

##### Share on other sites

Just add '.toFixed(6)' to final part of your code that will produce the end calculated result.

##### Share on other sites

It doesn't work

I also tried to put "volt.toFixed(6)" and nothing.

it shows the same.

##### Share on other sites

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">
<meta charset="utf-8" />
<title>Ohm's law</title>

<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 by dsonesuk
##### Share on other sites

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">
<meta charset="utf-8" />
<title>Ohm's law</title>

<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>
##### Share on other sites

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.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.