# Thread: Convert Excel formula to Javascript

1. Registered User
Join Date
Dec 2009
Posts
51

## Convert Excel formula to Javascript

Hi

Can anyone help me convert this formula to Javascript:

=IF((E9*0.05)>2, E9*0.05, 2)
(IF(logical_test,value_if_true,value_if_false))

Not sure how to go about the if statement and the value if true function in Javascript.

Hope some one can help!

Many thanks!

2. Member
Join Date
May 2013
Location
USA
Posts
9
use if else condition in nested form, try it may help you

3. Uses 'onblur' to effect change, but you can alter to some other event ...
Code:
```<!DOCTYPE HTML>
<html>
<title> Untitled </title>
<meta charset="utf-8">

<body>
<table border="1"><tr>
<td><input type="text" id="E9b" value=""></td>
</tr></table>

<script type="text/javascript">
v = Number(v);
if (v >= 2) { document.getElementById('E9b').value = (v * 0.05).toFixed(2); }
else { document.getElementById('E9b').value = 2; }
}
</script>

</body>
</html>```

4. Registered User
Join Date
Dec 2009
Posts
51
JMRKER thanks so much for your help!!

There was a mistake in my formula! Sorry about that it was supposed to be:
=IF((E9*0.05)>2, E9*0.06, 2)

I have the formula answer going into a field called "result".

Is the figure entered into E9a going into 'v' variable?

Many thanks for your help again...
Hope you can help me further as I cannot get the formula to work!

Originally Posted by JMRKER
Uses 'onblur' to effect change, but you can alter to some other event ...
Code:
```<!DOCTYPE HTML>
<html>
<title> Untitled </title>
<meta charset="utf-8">

<body>
<table border="1"><tr>
<td><input type="text" id="E9b" value=""></td>
</tr></table>

<script type="text/javascript">
v = Number(v);
if (v >= 2) { document.getElementById('E9b').value = (v * 0.05).toFixed(2); }
else { document.getElementById('E9b').value = 2; }
}
</script>

</body>
</html>```

5. Response in RED below ...
Originally Posted by pc_mac
JMRKER thanks so much for your help!!

There was a mistake in my formula! Sorry about that it was supposed to be:
=IF((E9*0.05)>2, E9*0.06, 2)
So just alter the function.

I have the formula answer going into a field called "result".
You will need to show YOUR code. I assume the 'E9b' would be your 'result' field.
Is the figure entered into E9a going into 'v' variable? Yes

Many thanks for your help again...
Hope you can help me further as I cannot get the formula to work!
What is wrong with your code. What errors are you getting in the error console?
The program works fine as published in post #3. What are you using to trigger the event?
Without seeing what you are doing, I'm no better at giving a haircut through the mail than I am at debugging your code!

6. Registered User
Join Date
Dec 2009
Posts
51
Originally Posted by JMRKER
Response in RED below ...

What is wrong with your code. What errors are you getting in the error console?
The program works fine as published in post #3. What are you using to trigger the event?
Without seeing what you are doing, I'm no better at giving a haircut through the mail than I am at debugging your code!
I'm not getting any errors - nothing happens when I try to add the "E9*0.06, 2" in to the javascript code.

Where would I add this code?

I'm not fully understanding how this Javascript code works the same as the excel formula. Sorry, I hope you don't mind helping me out.

This was my 1st attempt:

if (parseInt(form. E9.value) * 0.05 > 2)
{ form.result.value = parseInt(form. E9.value) * 0.06 }
else { form.result..value = 2 }

Thanks again
Last edited by pc_mac; 06-24-2013 at 11:24 AM.

"E9" is not an element in the code I provided.
It does not use a <form> tag.

In some places you use form.result.value, in other you use form. E9.value (note the space)
and in still another you use form.result..value ( note the double dots). Cannot be all three syntax correct.
Note also that you do not define the form in your snippet.

And look at your error console.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•